Create a 2d matrix with two nested cycle

1 回表示 (過去 30 日間)
SYML2nd
SYML2nd 2020 年 9 月 1 日
回答済み: Rik 2020 年 9 月 1 日
Hi all,
I am trying to create a 2d matrix with two nested cycle. My code is really complex, so I have written a very simple version of the problem. The scope of this simplified code was to obtain
[1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4]
So I have written a code like this
A=[]
B=[]
for x=1:1:4
for y=1:1:4
B=[B;y]
end
A=[A,B]
end
The problem seems to me that the nested cycle continue to append the value of y also once the cycle is ended. So when I do A=[A,B], I have the error that the array concatenated are not consistent.
I hope you can help me.
PS I know that this example of matrix can be obtained very easily, I want to obtain it using the nested cycles.

採用された回答

Rik
Rik 2020 年 9 月 1 日
Dynamically growing arrays is a bad idea. You should probably provide more details about your actual goal, because this problem definition doesn't make sense.
A=[];
for x=1:1:4
B=[];%reset B every loop of A
for y=1:1:4
B=[B;y];
end
A=[A,B];
end
A

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by