Finding minimum value in a 2D Matrix with a region
3 ビュー (過去 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;
0 件のコメント
回答 (1 件)
Guillaume
2015 年 1 月 26 日
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 件のコメント
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 Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!