How do I create a user generated matrix in Matlab?

246 ビュー (過去 30 日間)
Patrick Spencer
Patrick Spencer 2020 年 4 月 10 日
回答済み: Aryan 2024 年 8 月 8 日
Hello,
I am a student who has been tasked with writing a program that asks the user to define how many rows and columns a matrix should have. After which point, the user will be again asked what values go in each cell of the matrix. Where I am having trouble, is figuring out how to take the number of rows and columns that a user defines, and turn that into a matrix.
This is what I have so far:
Rows = input('Please input the number of rows you would like to have: ');
disp(' ');
Columns = input('Please input the number of columns you would like to have: ');
disp(' ');
If, for example the user wanted a matrix with 1 row and 4 columns, how would I go about generating said matrix given the user input?
Edit: Is it possible to achieve what I am asking, without simply having a user input a whole matrix?
  2 件のコメント
Mohsin
Mohsin 2022 年 9 月 24 日
Yes you can acheive without asking user, here is how
Matrix = randi([3,10],3,3)
It will create a matrix of order 3x3 and of number between 3 and 10
Feza Güngör
Feza Güngör 2023 年 11 月 28 日
useful bro, thanks !

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

採用された回答

David Hill
David Hill 2020 年 4 月 10 日
M = input('Input matrix: ');
%then input [1,2;3,4;5,6;7,8;9,10] for a matrix of 5 rows and 2 columns, or whatever matrix you want.
  9 件のコメント
Rohit D Kashyap
Rohit D Kashyap 2021 年 10 月 15 日
Is there any way of adding user generated matrix in loop.
Say i Want to ask the user to add n-number of matrix. How do i do that??
Sara Marium
Sara Marium 2022 年 3 月 26 日
thank u so much! :)

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

その他の回答 (3 件)

Abhishek Pawarq
Abhishek Pawarq 2021 年 8 月 23 日
a = input('Enter the number of rows');
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
b = input('Enter the number of coloumns');
for i=1:a
for j=1:b
p(i,j)=input('Enter the elements');
end
end
p=reshape(p,a,b)
  1 件のコメント
Sarah
Sarah 2023 年 2 月 26 日
is there a way to combine this into on statment for example [x,y] = input('Enter rows and columns')

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


JEERU
JEERU 2023 年 9 月 29 日
a = input('Enter the number of rows');
b = input('Enter the number of coloumns');
for i=1:a
for j=1:b
p(i,j)=input('Enter the elements');
end
end
p=reshape(p,a,b)

Aryan
Aryan 2024 年 8 月 8 日
a = input('Enter the number of rows');
b = input('Enter the number of coloumns');
for i=1:a
for j=1:b
p(i,j)=input('Enter the elements');
end
end
p=reshape(p,a,b)

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by