MATLAB Answers

How do I create a square matrix based on user input?

2,300 views (last 30 days)
Erin
Erin on 21 Feb 2014
Commented: Walter Roberson on 18 Jan 2021
I'm trying to write a program that asks the user what the dimensions of their square matrix are then takes that number, asks for each entry and creates the matrix for the program to use for the rest of the questions. I know how to create one row at a time, but I can't figure out how to put all the rows together to make one matrix.

  1 Comment

Akash h.r
Akash h.r on 16 Aug 2016
m=input('row')
n=input('column')
for i=1:m
for j=1:n
a(i,j)=input('elements-')
end
end
a=reshape(a,m,n)

Sign in to comment.

Answers (7)

Azzi Abdelmalek
Azzi Abdelmalek on 21 Feb 2014
M=[row1;row2;row3]

  1 Comment

Erin
Erin on 21 Feb 2014
But how do I write a program that reads a matrix that I prompt someone to enter? I'm pretty sure I have to do it one component at a time, I just can't figure out how to put them in the right places. My code looks like this:
C=input('How many columns are there in P? ');
R=input('How many rows are there in P? ');
P=zeros(R:C);
co=1;
ro=1;
while co<=C && ro<=R;
if co==1
P(co)=input('What is the first value of this column of P? ')
co=co+1;
elseif co>1
P(co)=input('What is the next value of this column of P? ')
co=co+1;
end
ro=ro+1;
end
P=P(R:C)
and the output looks like:
How many columns are there in P? 2
How many rows are there in P? 2
What is the first value of this row of P? .2
P =
0.2000 0
0 0
What is the next value of this row of P? .2
P =
0.2000 0
0.2000 0
P =
0.2000

Sign in to comment.


Anuj
Anuj on 21 Feb 2014
One simple way for this can be, Suppose you have square matrix of order n. So you have n^2 elements. You can enter all the elements one by one and change it into square matrix like this-
n=input('order of your square matrix is? ')
for i=1:n^2;
a(i)=input('elements-');
end
a=reshape(a,n,n)'
Regards

  0 Comments

Sign in to comment.


unknown nobody
unknown nobody on 15 Aug 2017
a = input('enter the matrix');
[1 2; 3 4]

  0 Comments

Sign in to comment.


Akash h.r
Akash h.r on 16 Aug 2016
m=input('row')
n=input('column')
for i=1:m
for j=1:n
a(i,j)=input('elements-')
end
end
a=reshape(a,m,n)

  0 Comments

Sign in to comment.


Bhupendra Raj Acharya
Bhupendra Raj Acharya on 6 Feb 2020
m=input('row')
n=input('column')
for i=1:m
for j=1:n
a(i,j)=input('elements-')
end
end
a=reshape(a,m,n)

  2 Comments

Josipa Petricevic
Josipa Petricevic on 5 Dec 2020
what I have to write in command window instead a(i,j)=input('elements-')?
Walter Roberson
Walter Roberson on 5 Dec 2020
The above code is valid in the command window -- though I would recommend putting in extra spaces,
m=input('row ')
n=input('column ')
for i=1:m
for j=1:n
a(i,j)=input('elements- ')
end
end
a=reshape(a,m,n)

Sign in to comment.


Amjad Alotaibi
Amjad Alotaibi on 19 Feb 2020
m=input('row')
n=input('column')
for i=1:m
for j=1:n
a(i,j)=input('elements-')
end
end
a=reshape(a,m,n)

  2 Comments

tbaracu
tbaracu on 18 Jan 2021
Can be read all the elements from the keyboard without to press ENTER after each element ? Lets say, I input direcctly with spaces from the keyboard 1 2 5 7, and the effect should be the matrix [ 1 2 ; 5 7] . Which is the procedure ?
Walter Roberson
Walter Roberson on 18 Jan 2021
https://www.mathworks.com/matlabcentral/answers/111358-input-function-to-enter-array-elements#comment_1268885

Sign in to comment.


Lam Nguyen Van
Lam Nguyen Van on 24 Mar 2020
help me. I want to input symbols matrix. ex: [a b c; f a h]

  1 Comment

Mehedi Hasan
Mehedi Hasan on 9 Sep 2020
How to take matrix from user. Calculate the mean, variance, covariance and standard
deviation for that matrix. Draw the histogram graph.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!