-3

我为一个大数组预分配内存,但是新数据附加在数组的末尾而不是覆盖数据我该如何解决这个问题。所以我可以为一个大数组预分配内存。

请注意数组是 44101x5001 我只是在示例中使用了较小的数字。

例子:

clear all
xfreq=zeros(10,10); %allocate memory

for ww=1:1:10
     xfreq_new = xfreq(:,1)+1+ww;
     xfreq=[xfreq xfreq_new]; %would like this to over write and append the new data where the preallocated memory of zeros are instead of appending to the end of it.
end

如果你运行它,你会注意到它附加了一个而不是覆盖了零。

阿罗哈瑞克

希望这能更好地解释事情分配数组

1)分配的内存为零

[0 0 0 0 0
0 0 0 0 0
0 0 0 0 0]

2)用数字覆盖分配的零内存,数字可以是任何东西,而不仅仅是数字一,我以数字一为例

[1 0 0 0 0
1 0 0 0 0
1 0 0 0 0]

3)仍然用数字覆盖分配的内存零,数字可以是任何东西,而不仅仅是数字一,我以数字一为例

[1 1 0 0 0
1 1 0 0 0
1 1 0 0 0]

问题在于这一行 xfreq=[xfreq xfreq_new]; %would like this to over write and append the new data where the preallocated memory of zeros are instead of appending to the end. 结尾

4

1 回答 1

3

如果您希望所有条目等于x

  x = (some number)
  A = zeros(10,n)
  for i=1:n
    A(:,i) = x;
  end

如果您希望您的列等于其他列,您必须这样做

  A = zeros(10,n)
  for i=1:n
    A(:,i) = v;
  end

其中 v 是大小为 (10,1) 的向量

于 2013-11-03T14:55:11.727 回答