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일

0 개 추천

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개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2020년 3월 17일

편집:

2020년 3월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by