[英]How do i save my output array from a loop?
I have a loop which outputs variable A
as an mx1 matrix (where: m>1)
for each iteration. 我有一个循环,它为每次迭代输出变量
A
作为mx1 matrix (where: m>1)
。 At the end of the entire loop, A
will end up as an mxn matrix
. 在整个循环的最后,
A
将最终成为mxn matrix
。 Although the output is returned as an mxn
matrix, it however keeps overwriting the results. 尽管输出以
mxn
矩阵形式返回,但仍会覆盖结果。 I have tried so many stuffs (including the ones that have worked for my previous codes) but none seem to work. 我尝试了很多东西(包括那些为我以前的代码工作过的东西),但似乎没有一个工作。 Nevertheless, i feel the code below should work but i'm not sure where the error is coming from.
不过,我觉得下面的代码应该可以工作,但是我不确定错误是从哪里来的。
x_A = NaN(28, 3); % 28=length of A and 3=length of kk
Z = [1,2,4,7];
for kk = 1: numel(Z)
[A,B,C] = fsave_output(Z, kk)
x_A = [x_A(:,kk) A(:,kk)];
% repeat the line above for B and C
end
When i run this, i get the error message: "Attempted to access x_A(:,3); index out of bounds because size(x_norm)=[28,2]." 当我运行此命令时,收到错误消息: “尝试访问x_A(:,3);索引超出范围,因为size(x_norm)= [28,2]。” .
。 However, the outputs in x_A(:,1) and X_A(:,2) are correct, only that x_A(:,3) doesn't show.
但是,x_A(:,1)和X_A(:,2)中的输出是正确的,只有x_A(:,3)没有显示。
Any ideas/suggestions/help on what i should do here? 关于我应该在这里做什么的任何想法/建议/帮助? Many thanks in advance!.
提前谢谢了!。
If you know the size of matrices to store before the loop, try 如果您知道循环之前要存储的矩阵大小,请尝试
Z = [1,2,4,7];
store_A = zeros(numel(Z), m); %m is known length
store_B = zeros(numel(Z), m); %if not known, unroll first iteration of loop
store_С = zeros(numel(Z), m); %or store it in a cell and concatenate after the loop
for kk = 1: numel(Z)
[store_A(kk,:),store_B(kk,:),store_C(kk,:)] = fsave_output(Z, kk)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.