How do I create a user generated matrix in Matlab?

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 commentaires

Mohsin
Mohsin le 24 Sep 2022
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
useful bro, thanks !

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

Is there anyway to make it a multi step process? Such that the user is asked about various aspects of the matrix, rather than simply inputting the entire matrix at once?
Such as:
How many rows?
How many columns?
What number would you like for cell 1/2/3...?
M=zeros(Rows,Columns);
for k=1:Rows
for m=1:Columns
M(k,m)=input(sprintf('Input the matrix value for (%d,%d): ',k,m));
end
end
That did the trick, thank you very much,
Would there be a way to output a comment if the determinant is zero?
Yes, but if you're thinking of using the determinant to decide if the matrix is singular that's not a good idea. A non-zero scalar multiple of the identity matrix isn't singular, is it? But dA says it is. The determinant of A underflowed to 0.
A = (1/64)*eye(500);
dA = det(A)
B is nearly singular and C is a non-zero multiple of B so it too should be nearly singular. But C's determinant is not close to 0.
B = [1 1; 1 1+eps]
det(B)
C = 1e10*B;
det(C)
Thank you! How would you implement this into the code above?
how to input each vector in a mtrix is a matrix sir?
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??
thank u so much! :)

Connectez-vous pour commenter.

Plus de réponses (3)

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 commentaire

Sarah
Sarah le 26 Fév 2023
is there a way to combine this into on statment for example [x,y] = input('Enter rows and columns')

Connectez-vous pour commenter.

JEERU
JEERU le 29 Sep 2023

0 votes

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 le 8 Août 2024

0 votes

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)

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by