How to find positive and negative outputs?

Hi, I'm trying to have a= dif<-100 or dif>100 with dif being the difference between two numbers, but having trouble getting the negative values to work.
I currently have it as
for i=0:5
dif=struct.smth(:,10) - struct.smth(:,i)
num = (dif <-100) | (100 < dif);
Set.Number =find(num);
end
I have also tried
dif=abs(dif);
num=100<dif;
Set.Number =find(num);
but still only get the positive values to work.
The output when the negative number is triggered is "0x1 empty double column vector"

6 Comments

Matt J
Matt J on 9 May 2022
Edited: Matt J on 9 May 2022
The find() command does not return negative numbers. It returns the indices (which are always positive) of the locations where the input is true.
Then woudn't the the dif=abs(dif) line work since all outputs are positive?
The output of both find() and abs() are always positive, regardless of their inputs. There is no reason to expect negative outputs from either of them.
Oh sorry I didn't clarify that when I'm saying negative output, I meant that when the differences is smaller than -100.
Final output is a positive output since struct.smth is a set of datas in the 1st quaderant.
I'm just trying to make that when the differences between the graph lines is greater than 100 or smaller than -100, it shows me the y axis it's on. I got it working for the positive differences but I'm not sure how to get the negative differences to work.
@Emily Maybe there are no elements of dif less than -100.
Can you provide a concrete example where the output of find is not what you expect?
" negative number is triggered is "0x1 empty double column vector"
It's always possible there ARE no values such that struct.smth(:,10) - struct.smth(:,i) < -100 in which case the empty vector is the expected result.
You need to always remember to write your code when using find and other functions which have such variable-number of outputs where, in fact, one of the valid results is that the result is NIL.
Often placing such code within the try...catch portion and the fixup code for the empty case in the catch...end portion of a try, catch block construct is a slick way to deal with the issue. It may be that the catch block is empty; there's nothing to do in that case.
Also NB:
ixG100 = find(abs(struct.smth(:,10) - struct.smth(:,i)) >100);
will find both cases; you can determine which easily enough when processing the results might be another way to approach it.
I didn't look at all at what really trying to do; just generic comments...

Sign in to comment.

 Accepted Answer

Emily
Emily on 10 May 2022
Values were being stored as uint16, changed the inputs to double to get it working.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 9 May 2022

Answered:

on 10 May 2022

Community Treasure Hunt

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

Start Hunting!