简体   繁体   English

如何从循环保存输出数组?

[英]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.

相关问题 如何让我的数组从firebase获得与我的静态数组完全相同的输出? - How do I get my array from firebase to have the exact same output as my static array? Shell脚本:如何将循环输出保存到数组 - Shell script: How to save loop output to array 我如何遍历这个关联数组并将其保存到数据库中? - how do i loop through this associative array and save it to the database? 如何使用 PowerShell 将数组中的项目添加到我的 output? - How do I add the items in my array to my output with PowerShell? 我如何解决这个数组和循环? 正确的输出但是 - How do i solve this array and loop? correct output but 如何循环遍历数组然后根据 .some() 的输出对其进行过滤? - How do I loop through an array then filter it based on the output of .some()? 如何将 if-loop 中生成的结果作为一个数组 output? - How do I output the result generated in if-loop as one array? 如何将循环中的变量设置到我的数组中,然后在不同的类中显示该数组? - How do I set a variable from a loop in to my array and then show the array in a different class? Matlab Simulink-如何从2级S函数输出数组? - Matlab Simulink - How do I output an array from my level 2 S Function? 如何在数组输出中添加换行符? - How do I add line breaks to my array output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM