在MATLAB中恢复矢量for循环 [英] Recovering vector in MATLAB for loop
问题描述
$ p $
for i = 1:n
v = zeros(1,i)
在$ i = n
之后结束
, v
将是一个n向量,但是我也想为任何 i
恢复向量。在我的代码中,每个向量 v
不是零行向量,而是大小不等的向量。感谢。
当Rody的帖子(+1)出现时,我已经输入了这个信息,太。一个替代的解决方案,效率稍低(我做了一些定时运行,差异是边际),而不是Rody的,但避免了复杂的索引是:
$ b $ pre $ $ code> A = cell(1,n);
for i = 1:n
A {1,i} =零(1,i);
end
Soln = cat(2,A {:});
我通过循环将不同长度的行向量存储在单元格数组中,然后在最后一步中将它们连接起来。
I am running a for loop in MATLAB. Each iteration produces a vector of length different than the vector created in the previous iteration. Is there any why to recover each individual vector? In the end I want to concatenate each of these vectors. My code is something like
for i=1:n
v = zeros(1,i)
end
so after i=n
, v
will be a one by n vector, but I also want to recover the vectors for any i
. In my code, each vector, v
, is not a zero row vector, but a vector of varying size. Thanks.
I'd already typed this when Rody's post (+1) came through so figured I might as well post it too. An alternate solution that is very slightly less efficient (I did some timed runs, the differences were marginal) than Rody's but avoids the complicated indexing is:
A = cell(1, n);
for i = 1:n
A{1, i} = zeros(1, i);
end
Soln = cat(2, A{:});
I store the varying length row vectors in a cell array through the loop then concatenate them in the final step.
这篇关于在MATLAB中恢复矢量for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!