matrix auto creation problem

If I want to make a matrix that inserts M as the order and it directly create a matrix with numbers from 1-M*M in that matrix. How should I like complete it
m=input('insert the degree of matrix')
for i=1:m
for j=1:m
a(i,j)=
endfor
endfor

回答 (1 件)

Aditya
Aditya 2025 年 1 月 23 日

0 投票

Hi Lucy,
To create a matrix where the elements are filled with numbers from 1 to M*M, you can use the 'reshape' function in MATLAB.
Here's is a sample MATLAB code:
m = input('Insert the degree of matrix: '); % Prompt the user for matrix size
a = reshape(1:(m*m), m, m); % Create and reshape the matrix
disp('The generated matrix is:');
disp(a); % Display the matrix
Note that the above solution will fill the matrix column-wise. If you want to fill it row-wise, you need to take the transpose of the matrix:
a = reshape(1:(m*m), m, m)'; % Create and reshape the matrix, then transpose
If you want to fill it in some other order, you can do it using for loops.
I hope this helps!

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2019 年 4 月 30 日

回答済み:

2025 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by