简体   繁体   中英

How to select values in an array until a threshold value?

I will explain my problem. I have a 1x1701 sampled array "resampled_WF", once I found the max value of this array ("peak_WF"), I set a threshold 0.7*peak_WF, and I would like to select the farthest value in the array that gets nearer to this threshold. Example: power_vs_time_threshold

As you can see, I was able to select ony the first value that resembles... but I would like to get the last one (around t=2 sec). I tried to flip the array with "flip" function:

WF_threshold_input = 0.7*peak_WF;
flip_resampled_WF = flip(resampled_WF);
diff_peak_threshold = peak_WF - WF_threshold_input; %power loss at 70% power reduction
diff_peak_WF = peak_WF - flip_resampled_WF;
min_diff_threshold = min(abs(diff_peak_WF-diff_peak_threshold));

Doing that, MATLAB computes minimum difference on the whole array, I would like to stop at the first value, not considering further values. I tried to select values with values <= WF_threshold_input, but again it selects over the whole dataset.

How can I select the value properly?

Thanks!!

Using operations on the matrix will call the operation on every element in the matrix. What you want to do is use a loop so that you can control exactly what elements are examined and then break out of the loop.

index = length(resampled_WF);

your_threshold = ...

while resampled_WF(index) < your_threshold
   index = index - 1;
end

The while loop will continue iteration until it reaches a value that is outside of your defined threshold.

After execution, the value of index will be the index of the furthest value in the array which is outside of your threshold. You can access the furthest value outside the threshold by looking at resampled_wf(index) after execution of the code.

We don't have to worry about the values of index leaving the bounds of the array, ie <1, since the condition resampled_wf < your_threshold is guaranteed to be met by the maximal value that you originally generated the threshold with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM