Constructing a circular type of matrix

1 回表示 (過去 30 日間)
Vatsala Sharma
Vatsala Sharma 2020 年 3 月 17 日
編集済み: Stephen23 2020 年 3 月 17 日
I want a matrix that is of the form:
Right now I am doing it as follows:
w = rand(M,N)
for i = 1:M
for j = 1:N
u = max(i,j);
w(i,j) = u;
end
end
But I don't like using the double for loops. Is there another way?

採用された回答

Stephen23
Stephen23 2020 年 3 月 17 日
編集済み: Stephen23 2020 年 3 月 17 日
For MATLAB versions >=R2016b:
>> M = max((1:4).',1:5)
M =
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
For earlier versions:
>> [X,Y] = ndgrid(1:4,1:5);
>> M = max(X,Y)
M =
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by