matlab在矩阵后面添加两行数据,如何在MATLAB中扩展矩阵的行以有效填充第一行的值添加的行...

I have a matrix myVel that is of size [1 501] meaning 1 row and 501 columns.

I want to extend this matrix so that the matrix will be of size [N 501], where N is an arbitrary number.

Each of the values in the columns need to be the same (meaning that all the values in the first column are all say, x and all of the values in the second column are say, y and so on).

This means that each row would consist of the same values.

How can I achieve this efficiently?

解决方案

Divakar's solution is one way to do it, and the link he referenced shows some great ways to duplicate an array. That post, however, is asking to do it without the built-in function repmat, which is the easiest solution. Because there is no such restriction for you here, I will recommend this approach. Basically, you can use repmat to do this for you. You would keep the amount of columns the same, and you would duplicate for as many rows as you want. In other words:

myVelDup = repmat(myVel, N, 1);

Example:

myVel = [1 2 3 4 5 6];

N = 4;

myVelDup = repmat(myVel, N, 1);

Output:

>> myVel

myVel =

1 2 3 4 5 6

>> myVelDup

myVelDup =

1 2 3 4 5 6

1 2 3 4 5 6

1 2 3 4 5 6

1 2 3 4 5 6

In general, repmat is called in the following way:

out = repmat(in, M, N);

in would be a matrix or vector of values you want duplicated, and you would want to duplicate this M times horizontally (rows) and N times vertically (columns). As such, for your case, as you have an array, you will want to duplicate this N times vertically and so we set the first parameter to N. The second parameter, the columns stay the same so we specify this to be 1 as we don't want to have any duplications... and thus the call to repmat you see above.

For more information on repmat, check out this link: http://www.mathworks.com/help/matlab/ref/repmat.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值