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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by