How do i build a matrix asking the user for inputs?
2 views (last 30 days)
Show older comments
this is script thus far. I asked the user for the number of lines and columns but when the matrix is 3 by 3 the asnwer given by the script is a matrix with 4 columns and 2 lines. HELP
num.linhas=input('Number of lines in the matrix: ');
num.colunas=input('Number of colums in the matrix: ');
for i=1:num.linhas
M(i)=input('Number: ');
end
for j=1:num.colunas
N(j)=input('Number: ');
end
A=[M;N];
A
idx=A==0;
contador=sum(idx(:));
size(A);
sz=size(A);
N=(num.linhas.*num.colunas);
percentagem=(contador./N)*100;
fprintf('\n A matriz A apresenta %f zeros',contador)
fprintf('\n A matriz tem uma dimensão de %f linhas e %f colunas',sz)
fprintf('\n A matriz A apresenta uma percentagem de %0.1f zeros',percentagem)
0 Comments
Answers (1)
David Hill
on 22 Jan 2021
If allowed, I would build the entire matrix with one input.
yourMatrix=input('Input your matrix in the given format, for example [1,2,3;4,5,6]: ');
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!