Replacing elements of a logical index vector

5 ビュー (過去 30 日間)
Kashmir Singh
Kashmir Singh 2016 年 2 月 10 日
編集済み: Jos (10584) 2016 年 2 月 10 日
“Idx” is a logical index vector when a certain “power on” condition is true (e.g. idx=Pwr_on>threshold) Say the result is 5 "zeros" and 5 "ones" per cycle i.e. idx=[0;0;0;0;0;1;1;1;1;1;0;0;0;0;0;1;1;1;1;1;0;0;0;0;0.........] Every time idx is true; I need to replace the first element and last element with “0” within the index. So effectively idx=[0;0;0;0;0;0;1;1;1;0;0;0;0;0;0;0;1;1;1;0;0;0;0;0;0.........] Any suggestions
  1 件のコメント
Adam
Adam 2016 年 2 月 10 日
編集済み: Adam 2016 年 2 月 10 日
How are you defining "first element and last element...within the index" for a general case? It is clear in this example, but is this as complicated as your data gets? i.e. there are always n 0's followed by n 1's and then this just cycles every 2n elements?

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

採用された回答

Image Analyst
Image Analyst 2016 年 2 月 10 日
This will do it. It's explicit so you can see what's going on. Of course, you could collapse it all down into a single line of code (though that may be hard to follow).
idx=[0;0;0;0;0;1;1;1;1;1;0;0;0;0;0;1;1;1;1;1;0;0;0;0;0]
di = [0; diff(idx)]
leadingEdges = find(di>0)
trailingEdges = find(di<0)-1
idx(leadingEdges) = 0;
idx(trailingEdges) = 0

その他の回答 (2 件)

Kashmir Singh
Kashmir Singh 2016 年 2 月 10 日
There's always zero's followed by one's. However the power duty cycle varies i.e. it's not always 5 zero's followed by 5 one's. This can be varied by increasing/decreasing the duty cycle.

Jos (10584)
Jos (10584) 2016 年 2 月 10 日
編集済み: Jos (10584) 2016 年 2 月 10 日
X = logical([0 1 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 1])
Y = fliplr(conv(fliplr(conv(double(X),[1 1],'same')),[1 1],'same'))>3

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by