converting a matrix into a column vector using only while-end loop
이전 댓글 표시
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
Jon
2022년 4월 27일
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
2022년 4월 28일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!