# Determining which came first, Min or Max and then subtracting

4 views (last 30 days)
m j on 13 Sep 2016
Commented: Jan on 1 Mar 2017
My goal is to compare which came first, the max value or minimum value, I use bsxfun() to find which index came first(max or min). But now I want to subtract the max or min value from each other, depending on which one came first.
So say max came first, so max value came before min value in this column, Now subtract max value from min value givin we know where they are located bc of indexs.
But im having trouble using the boolean logic to generate this form using bsxfun(), any ideas?
So with this:
[max_value1 max_index1] = max(data1);
[max_value2 max_index2] = max(data2);
[min_value1 min_index1] = min(data1);
[min_value2 min_index2] = min(data2);
%%compare max/min indexs
comInput1 = bsxfun(@lt, max_index1, min_index1); %%if max index is before min, then = 1
comInput2 = bsxfun(@lt, max_index2, min_index2);

John BG on 13 Sep 2016
do you mean this?
data1=randi([-10 10],1,10)
[max_data1 max_index]=max(data1);[min_data1 min_index]=min(data1);
if max_index>=min_index d=max_data1-min_data1; end
if max_index<min_index d=min_data1-max_data1; end

Jan on 24 Feb 2017
John BG has accepted his answer. I doubt that it solves the asked question, because "troubles with the boolean logic" implies, that this is not a scalar problem.
John BG on 1 Mar 2017
if you doubt it means you haven't checked.
If you had checked you would see it works, do you wanna bet?
Jan on 1 Mar 2017
I have checked it. While the original question seems to concern matrices, your question handles vectors only. Therefore I disagree that this answer should be accepted.

Jan on 26 Feb 2017
The appearence of bsxfun in the question might mean, that the input is an array, not a vector. Then:
data1 = rand(3, 4);
[max_value1, max_index1] = max(data1);
[min_value1, min_index1] = min(data1);
comInput1 = (max_index1 < min_index1);
result = (min_value1 - max_value1) .* (2 * comInput1 - 1);

#### 1 Comment

Image Analyst on 26 Feb 2017
Reading between the lines of the unclear, ambiguous question, I'm wondering if what "m h" actually wanted was to align the two signals. So that when they said "subtract", perhaps they actually meant "shift" (like circshift()), so that some part of one or both signals aligns with some other part of the signal.