Extracting the index numbers of greater than zero values of an array in different / separate sets MATLAB

123 ビュー (過去 30 日間)
I have an array of n length. Array has braking energy values. Index numbers represents Time in seconds.
Structure of array is as following:
1 to 140 index numbers array has zero values. (The time during which vehicle was not braking)
141 to 200 index numbers array has random energy values. (The time when vehicle was braking and regenerating energy)
201 to 325 index number array has zero values. (The time during which vehicle was not braking)
326 to 405 index numbers array has random energy values. (The time when vehicle was braking and regenerating energy)
and so on. This sequence goes on till nth number of array.
What I want to do is to get starting and ending index number of each set of energy values.
For example the result I must get shall be like
141 - 200
326 - 405
so on...
Latter this time will be used for charging of battery.
Can someone please suggest what method or technique shall I use to get this result.
  2 件のコメント
Image Analyst
Image Analyst 2018 年 12 月 4 日
Please attach some data for us to work with. Make it easy for us to help you, not hard. This is trivially easy with regionprops() if you have the Image Processing Toolbox.
Shahab Khan
Shahab Khan 2018 年 12 月 4 日
I have attached the vector i am working with.
Thanks

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2018 年 12 月 4 日
編集済み: Guillaume 2018 年 12 月 4 日
startends = find(diff([0, yourarray > 0, 0]));
startends = reshape(startends, 2, [])';
startends(:, 2) = startends(:, 2) - 1
  3 件のコメント
Guillaume
Guillaume 2018 年 12 月 4 日
編集済み: Guillaume 2018 年 12 月 4 日
I have no idea what I was thinking when I wrote the second line. It's not what I intended at all.
Fixed now. You understood how the code works anyway, judging by your comments.
Note that if energyB is a column vector, then:
startends = find(diff([0; energyB > 0; 0]));
will be more efficient than transposing. The rest would stay the same.
Shahab Khan
Shahab Khan 2018 年 12 月 4 日
Great It works perfectly. Thanks for your help.

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 12 月 4 日
編集済み: madhan ravi 2018 年 12 月 4 日
Use logical indexing:
idx=find(values>0) %gives linear indices
values(values>0) %gives you all the values greater than zero

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by