Create FOR loop to insert the matrix elements row-wise
Afficher commentaires plus anciens
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)
Réponses (3)
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
Walter Roberson
le 16 Août 2020
% 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 commentaires
Venkata Rama Krishna Gona
le 16 Août 2020
I have the values as following
F01=[10;20]
F02=[0;0]
F03=[20;30]
F04=[0;0]
for this i wrote following code
Xp=zeros(f,2);
F=zeros(f,2);
Nu=zeros(1,f);
f=2;
i=1;
while i<=f
disp(['For force F01',num2str(i)]);
F0(i)=input('enter the amplittude of forces column matrix in row wise--');
i=i+1;
end
Where each time the value is overriding in 'i' but i dont want to. please help me what to do.Thanks
Walter Roberson
le 16 Août 2020
Are you expecting the user to enter a row of values each time, and are you expecting that the first time the variable F01 will be affected, and that the second iteration the variable F02 would be affected ?
tbaracu
le 16 Août 2020
tbaracu
le 16 Août 2020
tbaracu
le 16 Août 2020
0 votes
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!