Extracting the data
조회 수: 1 (최근 30일)
이전 댓글 표시
hi,i have a vector consisting of a very few non zero values and remaining all zeros, i need to extract only the non zero values but not zeros. I write a code like x=[0 0 0 0 0 1 0 5 0 9 6 0 0 0 0] n=length(x); for i=1:n if(x(i)~=0) y(i)=x(i); end end But it'll result something like y=[0 0 0 0 0 1 0 5 0 9 6 0 0 0 0] But, i need only non zero values. Can anyone tell me how to get only the non zero values from the vector.
thanks & regards, sravan
댓글 수: 0
채택된 답변
Andrei Bobrov
2012년 2월 29일
y = x(x~=0)
or
y = nonzeros(x)
way bad with loop
out = [];
for j1 = 1:numel(x)
if x(j1) ~= 0
out = [out;x(j1)];
end
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!