i need to delete a zero from a single row 10 column vector

조회 수: 2 (최근 30일)
Omar Almahallawy
Omar Almahallawy 2019년 4월 28일
편집: Image Analyst 2019년 4월 28일
how can i delete this first 0
1.0e+03 *
0 1.0794 0.9376 0.8301 0.7477 0.6838 0.5939 0.5366 0.4988 0.4732
so it would look like this
1.0e+03 *
1.0794 0.9376 0.8301 0.7477 0.6838 0.5939 0.5366 0.4988 0.4732

채택된 답변

Stephan
Stephan 2019년 4월 28일
A = [0 1 2 3 0 5]
A = A(A~=0)
  댓글 수: 3
madhan ravi
madhan ravi 2019년 4월 28일
편집: Image Analyst 2019년 4월 28일
To delete the first zero entry ONLY:
A(find(A==0,1,'first'))=[];

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2019년 4월 28일
편집: Image Analyst 2019년 4월 28일
To delete only the FIRST zero (not all zeros) here is one way.
indexes = find(v == 0);
if ~isempty(indexes) % Only try to delete if we actually FOUND a zero!
v(indexes(1)) = []; % Do the deletion.
end
  댓글 수: 2
madhan ravi
madhan ravi 2019년 4월 28일
편집: madhan ravi 2019년 4월 28일
v(indexes(1) = []; % Do the deletion.
^--- missed a paranthesis
Image Analyst
Image Analyst 2019년 4월 28일
You're right - thanks - fixed.

댓글을 달려면 로그인하십시오.


dpb
dpb 2019년 4월 28일
As we've covered the other cases pretty thoroughly...
For the specific input vector OP showed where it is almost certain it is known a priori the first value in the array is zero from outside knowledge of what created the vector in the first place, simply
A=A(2:end);

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by