在MATLAB中恢复矢量for循环 [英] Recovering vector in MATLAB for loop

查看:161
本文介绍了在MATLAB中恢复矢量for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MATLAB中运行一个for循环。每次迭代产生一个长度不同于前一次迭代中创建的向量的向量。有没有什么为什么要恢复每个单独的载体?最后,我想连接每个这些向量。我的代码是类似于

$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆