Hey folks I'm trying to write a loop that will create arrays filled with zeros and I'd like it so that each array can be identified somehow (i.e. subscripts, unique name for each array).
In Maple, I would have done something like this... for k to 10 do A[k] := Array(1..10): end do:
which would create 10 arrays and each could be indivually accessed using A[k]. (i.e. A[1] is an array of size 10, A[2] is a different array of size 10 etc...)
Is there a way to do this in Matlab?

 採用された回答

Jos (10584)
Jos (10584) 2011 年 2 月 10 日

3 投票

If all the arrays are vectors (=1D arrays in matlab) you can store them in a matrix (=2D array).
% Pre-allocate an matrix with four "vector arrays" of 6 elements each
A = zeros(4,6) ;
Here is an example to fill this array
for k=1:4
A(k,:) = (1:6) ./ k ; % just an example
end
To access a "array" (vector!) use indexing B = A(2,:) % get second row
If the vectors have different sizes, you need to look into cells and/or structs
A = {1:3 1:10 1:5} % a cell with 3 vectors
A{3} % third vector

2 件のコメント

Walter Roberson
Walter Roberson 2011 年 2 月 10 日
Small modification: instead of
A(k,:) = [1:6) / k;
better is
A(k,:) = (1:6) ./ k;
1:6 is automatically a vector and does not need [] around it. And using ./ instead of / is good practice as is makes it clearer that you are expecting element-by-element division rather than matrix division.
Jos (10584)
Jos (10584) 2011 年 2 月 11 日
I completely agree, Walter. I have changed the answer accordingly.

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

その他の回答 (3 件)

Sean de Wolski
Sean de Wolski 2011 年 2 月 10 日

1 投票

doc cell
You need cell arrays.
Brian
Brian 2011 年 2 月 10 日

0 投票

Ok great thanks for that.
I think I'll go with Jos' option as all vectors should be the same size.

1 件のコメント

Walter Roberson
Walter Roberson 2011 年 2 月 10 日
These remarks are better done as Comments on the appropriate answer.

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

Hillary Omuga
Hillary Omuga 2020 年 1 月 31 日

0 投票

write a for loop to create an array named X. Each value in X is equal to its assaciated index value (a.k.a its position in the array) times 12 minus 1. The size of X is 52*1

カテゴリ

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

タグ

質問済み:

2011 年 2 月 10 日

回答済み:

2020 年 1 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by