How to remove numbers from a vector?

조회 수: 7 (최근 30일)
rafe brooks
rafe brooks 2020년 12월 30일
댓글: rafe brooks 2021년 1월 5일
I am using graphical data and looping round a set of data. So, how can I write an if statement to remove zeros from the start and end of the vector so that only the needed data is there, I dont want to remove all zeros though as the middle section of all the data sets has zeros which are needed?
The variable that the data sets loop through is called z_force

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 30일
minidx = find(z_force, 1, 'first')
maxidx = find(z_force, 1, 'last')
z_force(minidx:maxidx)
  댓글 수: 2
Image Analyst
Image Analyst 2020년 12월 30일
minidx = find(z_force, 1, 'first')
maxidx = find(z_force, 1, 'last')
z_force = z_force(minidx:maxidx) % Extract and assign back to z_force, overwriting the original.
rafe brooks
rafe brooks 2021년 1월 5일
Thanks for the help, thats great!

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

추가 답변 (1개)

William
William 2020년 12월 30일
You could use something like:
while z_force(1)==0
z_force(1) = [];
end
while z_force(end)==0
z_force(end) = [];
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by