Saving a variable with an index in a for loop

Hello. I am running into an issue of saving a created matrix with an index added to it for example
for i=1:10
x=1:10
y=1:10
z=1:10
A=[x y z]
end
What im tryng to do is then save the resulting matrix as A1, A2, A3 ect. any pointers for this?

回答 (1 件)

Steven Lord
Steven Lord 2021 年 10 月 4 日

0 投票

Can you do this? Yes.
Should you do this? The general consensus is no. See that Answers post for an explanation and alternatives.

3 件のコメント

Brooks Lane
Brooks Lane 2021 年 10 月 4 日
Hmm, Ive looked at your link and I'm a little lost tbh. What woould you recommend to do then if the arrays x y z being made are not consistent? I tried something such as :
A(:,:,i) =[x y z];
But am getting: Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Any inputs?
Walter Roberson
Walter Roberson 2021 年 10 月 4 日
If you do not have consistency of sizes, then use a cell array
A{i} = [x y z]
Stephen23
Stephen23 2021 年 10 月 5 日
編集済み: Stephen23 2021 年 10 月 5 日
The solutions given above concatenate your data, which apparently your data is not suitable for.
You can trivially resolve this by using a cell array, for example:
c = cell(10,3);
for ii = 1:10
c{ii,1} = 1:10;
c{ii,2} = 1:10;
c{ii,3} = 1:10;
end
Your approach of putting pseudo-indices into the variable names should definitely be avoided.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

タグ

質問済み:

2021 年 10 月 4 日

編集済み:

2021 年 10 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by