How to save the variables inside for loop

I am executing loop for k=1,2,5,6,.......100 E=produces column vector of different size; end
I need to save 'E' for each value of k;
Thanks Any help is appreciated!

 採用された回答

jgg
jgg 2016 年 1 月 25 日
編集済み: jgg 2016 年 1 月 25 日

3 投票

The easiest way to probably to just use a cell array.
Es = cell(100,1)
for i = 1:100 %your loop
% do stuff; generate E
Es{i} = E
end
I'm assuming you already have the loop working, you're just trying to save the output. This will be stored in Es.

3 件のコメント

Jab
Jab 2016 年 1 月 25 日
Thank you very much! Could we do something in vector without cell operation? Thanks
jgg
jgg 2016 年 1 月 25 日
It would probably be difficult; all your vectors are of different sizes, which means you can't just stack them into a big matrix. If you knew they had a max size M then you could do this:
Es = NaN(100,M);
for i = 1:100 %your loop
%do stuff, generate E
Es(1:length(E)) = E;
end
This will store E in the first elements of Es for each row; the rest will be NaN values. The problem is that this is pretty inefficient in terms of memory, but it might work. You also need to know M which is difficult.
Michael O'Brien
Michael O'Brien 2019 年 11 月 2 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

Jab
2016 年 1 月 25 日

コメント済み:

2019 年 11 月 2 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by