0

现在,我明白了这里的问题,但我不明白的是应该如何初始化“我”?

例如,在我的例子中,变量“p”在每个 for 循环结束时生成一个数组,该数组被编程为运行 101 次。p 的输出看起来像这样

p =

-0.0149 -0.0149 现在,我想在每个 for 循环结束时将此值存储到另一个变量中并增加其索引。

那么,该变量的维度应该是多少?

我试过初始化---> A=rand(2,101);

4

1 回答 1

2

If you don't wish to pre-allocate the array A. Then you can keep appending the values in A at the each for loop iteration as follows:

A=[];
for loop
   get p vector (every iteration it should be 2x1)
   A=[A p];
end

Or you can directly write in the for loop:

 A(:,i)=p; %but your p vector seems to be 1x2 and not 2x1. If it is 2x1, then you should initialize A as rand(101,2)
于 2013-03-27T03:02:33.977 回答