converting a matrix into a column vector using only while-end loop
Afficher commentaires plus anciens
Hi all, I'm trying to convert a matrix into a column vector. I also need to do this only using while loops. Please help. Here is my code:
function [A] = func4(M)
[m,n] = size(M);
A = zeros(m*n,1);
i = 1;
j = 1;
c = m*n;
while i <= m
cx = c;
A(cx,1) = M(m, n);
while j <= n
A(j,1) = M(i, j)
j = j + 1;
end
break
end
M = [1 2 3; 4 5 6]
B = func4(M)
2 commentaires
Jon
le 27 Avr 2022
First of all you can do the whole thing with just one statement
M = A(:)
Even if you were going to do it with loops it would be better to use a for loop than a while as you already know how many iterations you need to do.
Is this for a homework problem that requires you to use while loops?
Hrvoje Sarinic
le 28 Avr 2022
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing 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!