MATLAB Answers

using the find function using conditions

6 ビュー (過去 30 日間)
shobhit mehrotra
shobhit mehrotra 2015 年 10 月 6 日
編集済み: Kirby Fears 2015 年 10 月 6 日
I have data for heading (varies between 0 and 360 degrees) and I want to find every point that the heading crosses 240 degrees. Attached is a picture of the heading vs time. Since we are moving in circles the pattern is repeatable. I want to find each point the heading crosses 240 degrees, but I don't want the points where the heading resets from 0 to 360 degrees denoted by the vertical line in the plot.
This is what I have in my code, but its not finding anything
find(diff(hdg) < 25 & hdg(1:end-1) < 240 & hdg(2:end) > 240)
if I remove the
diff(hdg) < 25
condition I get results but its finding the indices where the heading resets from 0 to 360 degrees (vertical line), I don't want to include those points, just where the heading actually crosses 240 degrees. Also note that the hdg data is not continuous, it looks similar to this
hdg = [0, 3.2556, 6.3458, 9.2654, 12.66, 16.225, ......237.35, 240.33, ....357.22, 1.22, ...239.55, 243.33,....]
thanks

回答 (1 件)

Kirby Fears
Kirby Fears 2015 年 10 月 6 日
編集済み: Kirby Fears 2015 年 10 月 6 日
Try applying abs() to the diffs - you might have the direction of your circle wrong. Also one of those hdg inequalities should be "<=" or ">=" in case you cross 240 with a value of exactly 240. Also the value of "25" could probably be larger. When a reset from 0 to 360 occurs at reasonable sampling frequency, could you guarantee a jump of 200 or more? That would ensure you don't miss any valid 240 degree crossings.
idx = find(abs(diff(hdg)) < 200 & hdg(1:end-1) < 240 & hdg(2:end) >= 240);
If this doesn't work, you should save diff(hdg) to a variable and inspect it. Maybe plot it as well.
Hope this helps.

製品

Community Treasure Hunt

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

Start Hunting!

Translated by