Hii, I was trying to solve this simple problem. I have to create a matrix with first n.^2 elements: example for n=3 the matrix would be
1 2 3
4 5 6
7 8 9
I have used this strategy :
n=input('order of square matrix is? ')
for i=1:n^2;
A(i)=input('numer of elements:');
end
A=reshape(A,n,n)'
at this point I don't wanna enter the number of elements manually but I need the matrix to be created as soon as I enter the order of matrix. How can I do that?

 採用された回答

Torsten
Torsten 2023 年 4 月 5 日

0 投票

n = 3;
A = reshape(1:n^2,n,n).'
A = 3×3
1 2 3 4 5 6 7 8 9

2 件のコメント

Aleksandra MANGUSHEVA
Aleksandra MANGUSHEVA 2023 年 4 月 5 日
Thank you for your answer but the n=3 was just an example, the idea was putting a general n number and create a matrix from 1 to n.^2 elements, that is why I put an input at the beginning just because you choose the size of a quadrate matrix and then it displays you all elements from 1 to n.^2. Maybe I'm completley wrong about my code, I'm just searching the right idea.
Torsten
Torsten 2023 年 4 月 5 日
Then replace
n = 3
by
n = input('order of square matrix is? ')

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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