如何在MATLAB中扩展矩阵的行以有效填充第一行的值添加的行 [英] How to extend the rows of a matrix in MATLAB filling the added rows with the first row's values efficiently

查看:878
本文介绍了如何在MATLAB中扩展矩阵的行以有效填充第一行的值添加的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵myVel,其大小为[1 501],表示1行和501列. 我想扩展此矩阵,以便矩阵的大小为[N 501],其中N是任意数字.

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.

列中的每个值都必须相同(这意味着第一列中的所有值都说x,第二列中的所有值都说y,依此类推). 这意味着每一行将包含相同的值.

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的解决方案是做到这一点的一种方法,他引用的链接显示了一些复制数组的好方法.但是,该帖子要求在没有内置功能repmat的情况下执行此操作,这是最简单的解决方案.因为这里没有您的限制,所以我建议您使用这种方法.基本上,您可以使用repmat为您执行此操作.您将保持相同的列数,并根据需要复制任意多的行.换句话说:

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);

示例:

myVel = [1 2 3 4 5 6];
N = 4;
myVelDup = repmat(myVel, N, 1);

输出:

>> 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


通常,通过以下方式调用repmat:

out = repmat(in, M, N);

in是要复制的值的矩阵或向量,并且您希望水平复制此M次(行)和垂直复制N次(列).这样,对于您的情况,由于您有一个数组,您将希望垂直复制此N次,因此我们将第一个参数设置为N.第二个参数,各列保持不变,因此我们将其指定为1,因为我们不希望有任何重复...因此您可以在上面看到对repmat的调用.

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.

有关repmat的更多信息,请查看以下链接: http: //www.mathworks.com/help/matlab/ref/repmat.html

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

这篇关于如何在MATLAB中扩展矩阵的行以有效填充第一行的值添加的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆