Maximizing along n-1 dimensions of an n-dimensional array without a for loop
2 次查看(过去 30 天)
显示 更早的评论
I have an n-dimensional array, VV, which I would like to maximize along all dimensions for each element in the first dimension. I would also like to obtain the associated linear indices so that I can evaluate related arrays at those locations. I have to do this a large amount of times, so I need to figure out a way to do this without a for loop.
Here is an example for an n=3 dimensional array, but with a loop:
VV=rand(10,10,10);
for ii=1:size(VV,1)
Vtemp = squeeze(VV(ii,:,:));
[V(ii) I(ii)] = max(Vtemp(:));
end
[~,ind] = ismember(V,VV);
Any help would be appreciated.
0 个评论
采纳的回答
更多回答(1 个)
Steven Lord
2021-10-12
VV=randi(1000, 5, 5, 5);
[values, locations] = max(VV, [], 1:ndims(VV)-1, 'linear');
check = [reshape(values, [], 1), reshape(VV(locations), [], 1)]
1 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!