How do i build a matrix asking the user for inputs?

2 visualizzazioni (ultimi 30 giorni)
Fábio Sousa
Fábio Sousa il 21 Gen 2021
Risposto: David Hill il 22 Gen 2021
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)

Risposte (1)

David Hill
David Hill il 22 Gen 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]: ');

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by