Assigning a 2D matrix

41 ビュー (過去 30 日間)
Sariha Azad
Sariha Azad 2019 年 5 月 31 日
コメント済み: Sariha Azad 2019 年 6 月 1 日
I have to create a 2D matrix, where the row number is unknown. I will have to find the row number.
Say, for i=1:5
for j=1:3
I want to create a 2D matrix with 2 columns, where the entries will be like [i j]
How should I write the code?
  2 件のコメント
Raj
Raj 2019 年 5 月 31 日
Your question is not at all clear. What exactly are you looking for? Can you give a better example?
Sariha Azad
Sariha Azad 2019 年 5 月 31 日
I want the iterators as entries, as for i=1:2,for j=1:2, matrix=[1 1;1 2;2 1;2 2]

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

採用された回答

KSSV
KSSV 2019 年 5 月 31 日
If you know column numbers and don;t know row number; you can initialize as below;
iwant = zeros([],col) ;
If you know row numbers and dont know column number; you can initialize as below;
iwant = zeros(row,[] ;
But, it seems in your case:
iwant = zeros(5,3) ;
for i=1:5
for j=1:3
iwant(i,j) = rand ;
end
end
  1 件のコメント
Sariha Azad
Sariha Azad 2019 年 5 月 31 日
I want to make like for i=1:2,for j=1;2,
iwant=[1 1;1 2; 2 1; 2 2]

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

その他の回答 (2 件)

Sariha Azad
Sariha Azad 2019 年 6 月 1 日
Well, I got this one.
a=[0 0];
for i=1:5
for j=1:3
a=[a;i j];
end
end
disp(a)

Stephen23
Stephen23 2019 年 6 月 1 日
編集済み: Stephen23 2019 年 6 月 1 日
Simpler without a loop:
>> [X,Y] = ndgrid(1:3,1:5);
>> M = [X(:),Y(:)]
M =
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
1 4
2 4
3 4
1 5
2 5
3 5
  1 件のコメント
Sariha Azad
Sariha Azad 2019 年 6 月 1 日
Thanks

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

カテゴリ

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