Determining which came first, Min or Max and then subtracting

1 次查看(过去 30 天)
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);

回答(2 个)

John BG
John BG 2016-9-13
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
  3 个评论
John BG
John BG 2017-3-1
if you doubt it means you haven't checked.
If you had checked you would see it works, do you wanna bet?
Jan
Jan 2017-3-1
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
Jan 2017-2-26
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 个评论
Image Analyst
Image Analyst 2017-2-26
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by