且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MATLAB中扩展矩阵的行以有效填充第一行的值添加的行

更新时间:2023-08-28 19:57:46

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