How to assign values to a variable size array

The following works fine
k=[];
for jj=1:10
k(jj,:)=randi(10,1,3);
end
However, I'm facing difficulties when I try to assign different rows' elements to variable size array k:
k=[];
for jj=1:10
k(1,:)=randi(10,1,jj);
end
I would appreciate your help.

6 件のコメント

Walter Roberson
Walter Roberson 2021 年 2 月 21 日
Arrays cannot have different number of elements per row or column.
Asaf McRock
Asaf McRock 2021 年 2 月 21 日
Thank you Mr. Roberson.
Is it possible to get a row vector k as an output? that is, stack all random row vectors side by side in k
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 2 月 21 日
Are you considering this
k=[];
for jj=1:10
k=[k,randi(10,1,jj)];
end
Or
For rows or column vectors of different lengths, you can save the data in a cell array.
Asaf McRock
Asaf McRock 2021 年 2 月 21 日
This works perfectly. But I wonder how can I use a cell array inside a for loop for saving rows or column vectors of different lengths.
Thank you, Mr. Acharjya!
Walter Roberson
Walter Roberson 2021 年 2 月 21 日
N = 10;
k = cell(N,1);
for jj = 1:N
k{jj} = randi(10,1,jj);
end
k
k = 10x1 cell array
{[ 6]} {1×2 double} {1×3 double} {1×4 double} {1×5 double} {1×6 double} {1×7 double} {1×8 double} {1×9 double} {1×10 double}
Asaf McRock
Asaf McRock 2021 年 2 月 21 日
Thank you, gentlemen!
I'm learning so much from you. Much appreciated!

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

回答 (0 件)

カテゴリ

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

質問済み:

2021 年 2 月 21 日

コメント済み:

2021 年 2 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by