how to use output indexes from the MAX function
古いコメントを表示
Hi all,
I want to find the indexes of the maximum and the second maximum in 3D array for a certain dimension. My strategy is to use the MAX function for finding the indexes of the first maximum. Use these indexes to make the value of the 3D array –Inf and call again the MAX function in order to find the indexes of the second maximum of the 3D array
My question is how to make optimal use of the returned indexes from the MAX function (without for loops).
cube1 = 10*rand([5 6 7]); %create 3D array
cube2 = cube1; %make copy for com parison
[dummy i1st] = max(cube1,[],3); %find first max across 3th dimension
cube1(i1st)=-inf; % this wont work
% for loop does work but is not elegant, require recoding when dimension in changed
for r=1:size(i1st,1)
for c=1:size(i1st,2)
cube1(r,c,i1st(r,c)) = -inf;
end
end
[dummy i2nd] = max(cube1,[],3); %find second max across 3th dimension
Does anyone has some ideas how to get rid of the for loop?
Patrick
3 件のコメント
Walter Roberson
2011 年 12 月 10 日
Question: what do you want to do if there are multiple copies of the maximum?
Andrei Bobrov
2011 年 12 月 12 日
[v,idx] = sort(cube1,3);
dummy = v(:,:,end-1) ;
i2nd = idx(:,:,end-1);
Andrei Bobrov
2011 年 12 月 12 日
[~,i3] = max(cube1,[],3)
M1 = cube1;
s = size(cube1);
M1((i3-1).*prod(s(1:2))+reshape(1:prod(s(1:2)),s(1),[])) = -inf
[dummy,i2nd] = max(M1,[],3);
採用された回答
その他の回答 (1 件)
Raptrick
2011 年 12 月 12 日
カテゴリ
ヘルプ センター および File Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!