Hi everyone. I have to do a nxn matrix, whose elements are natural numbers between 1...n^2. For example, if n=3 the matrix will be:
[1 2 3; 4 5 6; 7 8 9]
Also, I have to be able to order the matrix by rows or columns.
Of course, thanks. I'm just starting with the Matlab world and it's not easy lol.

 채택된 답변

Chad Greene
Chad Greene 2015년 6월 12일

1 개 추천

Here's one way:
n = 3;
% Create a 1D array of all the values:
m = 1:n^2;
% Reshape m into nxn:
m = reshape(m,n,n)

댓글 수: 3

Facundo Beer
Facundo Beer 2015년 6월 12일
Thanks a lot! Now, with this method matlab orders the matrix by columns. How could I order it by rows?
Chad Greene
Chad Greene 2015년 6월 12일
Ah, oops! Thanks James.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 6월 12일

댓글:

2015년 6월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by