converting a matrix into a column vector using only while-end loop
Ältere Kommentare anzeigen
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 Kommentare
Jon
am 27 Apr. 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
am 28 Apr. 2022
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!