Develop a Matlab function that remove all zeros in an input vector(using while loop)
조회 수: 1 (최근 30일)
이전 댓글 표시
using while loop
댓글 수: 0
답변 (1개)
kowshik Thopalli
2017년 11월 27일
편집: kowshik Thopalli
2017년 11월 27일
I agree with cvklpstunc that you dont need a while loop. If you are still looking for a while loop implementation, here is the code
count=1;z=1;
a=[1:10, zeros(5,1)'];
while count<=length(a)
if a(count)==0
zero_indices(z)=count;
z=z+1;
end
count=count+1;
end
a(zero_indices)=[];
Here is one line answer for what you want to do
b=a(find(a))
댓글 수: 2
James Tursa
2017년 11월 27일
Please don't provide complete solutions to homework problems. Also your code has problems because it doesn't initialize zero_indices.
kowshik Thopalli
2017년 11월 30일
James Tursa- Thank you for the suggestion. Will certainly keep in mind from next time.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!