Finding minimum value in a 2D Matrix with a region

조회 수: 3 (최근 30일)
Vittal Rao
Vittal Rao 2015년 1월 26일
댓글: Guillaume 2015년 1월 26일
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에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by