フィルターのクリア

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

85 ビュー (過去 30 日間)
Erin
Erin 2014 年 2 月 21 日
編集済み: DGM 2023 年 3 月 3 日
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.

回答 (5 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 21 日
M=[row1;row2;row3]
  1 件のコメント
Erin
Erin 2014 年 2 月 21 日
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

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


Anuj
Anuj 2014 年 2 月 21 日
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

Akash h.r
Akash h.r 2016 年 8 月 16 日
編集済み: DGM 2023 年 3 月 3 日
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)
  4 件のコメント
tbaracu
tbaracu 2021 年 1 月 18 日
移動済み: DGM 2023 年 3 月 3 日
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 ?

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


unknown nobody
unknown nobody 2017 年 8 月 15 日
a = input('enter the matrix');
[1 2; 3 4]

Gowtham K
Gowtham K 2021 年 12 月 25 日
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)
b= a+a
c=a-a
m=a*a
s= a^2
  2 件のコメント
Gowtham K
Gowtham K 2021 年 12 月 25 日
not working for 2*2 matrix but working for 3*3 matrix
DGM
DGM 2021 年 12 月 25 日
編集済み: DGM 2021 年 12 月 25 日
The code you posted works for 2x2 just fine. It's entirely unnecessary, tedious and error-prone to force users to enter arrays using input(), but for some reason it's popular to teach people bad ideas like this.
If you have to do things this way, at least make it readable.
% use meaningful prompts with clear formatting and output suppression
m = input('number of rows: ');
n = input('number of columns: ');
a = zeros(m,n); % preallocate
for i=1:m
for j=1:n
a(i,j)=input(sprintf('enter a(%d,%d): ',i,j)); % again
end
end
%a = reshape(a,m,n) % this does nothing. the array is already this shape
b = a+a
c = a-a
m = a*a % this is matrix multiplication, not elementwise multiplication
s = a^2 % same

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by