Finding minimum value in a 2D Matrix with a region

1 次查看(过去 30 天)
I have a set of 18 matlab vectors(raw readings of current values) data put into a 2d matrix of B[ 18 * 16348].
I have found the indices and max value of each vector and stored in another vectors namely M( has max values of each row vector of B) and l (has indices of max of each row vector of B). [M,l]=max(B,[],2)
Now i want to find the least value of each row vector before this max value by looking backwards. This would give me the difference between max curretn and the least current before.
I wrote a loop to look backwards and check for the least value and its indices. Apparently it does not work.
Could someone help ? Below is what i am trying.
o=l;
for i=1:18
while o(i)>0
if( B(i,o(i))>B(i,o(i)-1))
o(i)=o(i)-1;
else
display(o(i));
display(B(i,o(i)));
break;
end;
break;
end;
end;

回答(1 个)

Guillaume
Guillaume 2015-1-26
If you don't need the indices of the minimums, it's fairly straightforward using cummin:
B = [5 2 3 7 6 1 3; 3 2 1 4 8 2 0] %just for testing
[M, I] = max(B, [], 2);
cm = cummin(B, 2);
minbefore = cm(sub2ind(size(cm), [1:size(cm, 1)]', I))
diffminmax = M - minbefore
  2 个评论
Vittal Rao
Vittal Rao 2015-1-26
Thanks for that. Semms to work. But i need the indices of minima too. Any work around ?
Guillaume
Guillaume 2015-1-26
This would work with the code I've posted:
minindices = cellfun(@(row, m) find(row == m, 1), num2cell(B, 2), num2cell(minbefore))
But in that case, you might as well replace the whole lot with:
[M, I] = max(B, [], 2);
[minbefore, minindices] = cellfun(@(row, i) min(row(1:i)), num2cell(B, 2), num2cell(I))

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by