2

I came across this code during some troubleshooting in Matlab when trying to find peaks of a signal:

minValue = min(yourSignal);
yourSignal(yourSignal < threshold) = minValue;

What I think it does is put the minimum element in the vector yourSignal in the first line. And then the next line I'm not sure what's happening, but it seems to assign yourSignal with any element less than threshold in the vector yourSignal. I don't understand what the assignment to minValue does.

Please explain to me what this code does or tell me the name of the operation in the second line so that I can find its documentation.

user50420
  • 39
  • 1
  • 6

1 Answers1

3

This is called logical indexing. You could break it into 2 operations

Idx = yourSignal < threshold;

That creates a logical array (true or false), which is true when the signal array is less than the threshold.

yourSignal(Idx) = minValue;

Anywhere that Idx is true, replace that element with minValue.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • great, thanks! i'm not particularly versed in matlab, so it's difficult to search something if you don't know the name of it. for posterity: https://blogs.mathworks.com/loren/2013/02/20/logical-indexing-multiple-conditions/ – user50420 Oct 21 '18 at 22:21
  • No worries, it certainly can be! Added your link to the answer. – Wolfie Oct 22 '18 at 08:09