필터 지우기
필터 지우기

Create FOR loop to insert the matrix elements row-wise

조회 수: 25 (최근 30일)
tbaracu
tbaracu 2020년 8월 16일
댓글: tbaracu 2020년 8월 16일
I want to create a matrix in an equivalent way as it is possible in C++: just to insert the elements one after the other with a loop like:
cout<<"\nEnter elem. of aug. matrix row-wise:\n";
for (i=1;i<=n;i++) {
for (j=1;j<=n+1;j++) {
cin>>a[i][j]; //input the elements of array
}
}
It is possible to make this in MAtlab ? Something like below, but without to press enter after each element:
% lets say, A=[1 2 ; 5 7 ] but I want just to write only 1 2 5 7 and press ENTER.
n=input('matrix dimension:')
for i=1:n
for j=1:n
a(i,j)=input(' Insert the elements row wise, one after the other:')
end
end
a=reshape(a,n,n)

답변 (3개)

KSSV
KSSV 2020년 8월 16일
편집: KSSV 2020년 8월 16일
n=input('number of elements = ') % n = 4
for i=1:n;
a(i)=input('elements-'); % 1 2 5 7
end
a=reshape(a,n/2,n/2)' % not should be even for this example
  댓글 수: 2
tbaracu
tbaracu 2020년 8월 16일
You are also close, but your code requires to press enter after each element.
I want just to write the dims of the matrix (n or n x m), then to input from keyboard the series 1 2 5 7 , press ENTER and the matrix to appear.
KSSV
KSSV 2020년 8월 16일
How about this? When elements is prompted, you enter [1 2 5 7]. Type your values in square brackets.
n=input('number of elements = ') % n = 4
a = input('elements-'); % [ 1 2 5 7]
a=reshape(a,n/2,n/2)' % not should be even for this example

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2020년 8월 16일
% lets say, A=[1 2 ; 5 7 ] but I want just to write only 1 2 5 7 and press ENTER.
n = input('matrix dimension:')
for i=1:n
s = input( sprintf('Insert the elements for row #%d, all on one row: ', i), 's');
a(i,:) = str2double(strsplit("[" + s + "]"));
end
  댓글 수: 4
tbaracu
tbaracu 2020년 8월 16일
Just to write all set of elements from the keyboard like 1 5 7 9... then press ENTER and the matrix to be considered by MAtlab.
Example: for matrix [ 1 5 ; 7 9] I want just to write after the FOR instruction is activated, just 1 5 7 9 and the 2 x 2 matrix to be completed.
tbaracu
tbaracu 2020년 8월 16일
Walter Roberson get closer, but still it is necessary an improvement, because the result looks like this:
matrix dimension:2
n =
2
Insert the elements for row #1, all on one row: 1 5
Insert the elements for row #2, all on one row: 7 9
a =
NaN NaN
NaN NaN

댓글을 달려면 로그인하십시오.


tbaracu
tbaracu 2020년 8월 16일
It should be something like Matlab after reading 1 5 7 9 initially considers the row matrix [ 1 5 7 9] then split it in two rows to make it [1 5 ; 7 9].
Or, in other example, given from keyboard -----> 1 3 4 7 5 6 1 8 6 -----> press ENTER -----> Matlab initially considers the row matrix [ 1 3 4 7 5 6 1 8 6 ] and split it in 3 rows according to the dim of the matrix to obtain [ 1 3 4 ; 7 5 6 ; 1 8 6 ].

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by