make matrix from arrays with for loop

Hello, I have the following problem:
array_1 = [1 2 3];
array_2 = [4 5 6];
array_3 = [7 8 9];
array_i = [.....];
I want to use a for loop to create one matrix looking like:
matrix =
1 2 3
4 5 6
7 8 9
.....
How can I realize that? Thanks, J.

3 件のコメント

KSSV
KSSV 2017 年 6 月 23 日
How you have these arrays? In works pace or in text file?
Jonas K
Jonas K 2017 年 6 月 23 日
in my workspace but both solutions would be interesting
Stephen23
Stephen23 2017 年 6 月 23 日
編集済み: Stephen23 2017 年 6 月 23 日
The most important question is: how did you get those arrays into your workspace? The best solution is to avoid creating such arrays... and luckily it is trivial to avoid when loading/importing data or when creating data. So, given that it is so trivial to avoid this problem, when not improve your code by avoiding this whole problem altogether?
About the worst solution would be to use eval (which some beginners love to use), you might like to first know what the MATLAB documentation says about it: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
You might also like to read what experienced MATLAB users say about what you are trying to do (hint: they strongly advise against it):
A much better solution is to load your data into one variable, and then simply access the data using indexing and/or fieldnames, e.g. if you use load then always load into an output variable. This will more efficient, neater, more robust, easier to check, easier to debug, faster,...

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

 採用された回答

KSSV
KSSV 2017 年 6 月 23 日

0 投票

%%create arrays
array_1 = [1 2 3];
array_2 = [4 5 6];
array_3 = [7 8 9];
array_4 = [1 2 3];
array_5 = [4 5 6];
array_6 = [7 8 9];
save myfile.mat ;
clearvars
S = load('myfile.mat') ;
names = fields(S) ;
iwant = zeros(length(names),3) ;
for i = 1:length(names)
iwant(i,:) = getfield(S,names{i}) ;
end

5 件のコメント

Jonas K
Jonas K 2017 年 6 月 23 日
Thanks! The save function saves ALL of my variables. I only want it to save the array_... variables. How do I select them without spelling the name of every single one?
Stephen23
Stephen23 2017 年 6 月 23 日
" How do I select them without spelling the name of every single one"
Read the save documentation and use the -regexp option.
Guillaume
Guillaume 2017 年 6 月 23 日
But, as Stephen commented in the question, the best solutions by far, is not to create these numbered variables in the first place.
Numbered variables are always a very good indication that you're doing something very wrong. I would focus on changing the way these are created rather than on clunky workaround such as eval or load + save (which in my opinion is just as bad as eval)
Jonas K
Jonas K 2017 年 6 月 23 日
works like a charm! you guys rock:)
KSSV
KSSV 2017 年 6 月 23 日
Jonas, you have to follow the Stephen comments. What he said is exactly true. This practise is not good idea.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2017 年 6 月 23 日

コメント済み:

2017 年 6 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by