How to use "imregionalmax" without using for loop
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, I have an 51×20×20 Array and I wish to find the second biggest peak in each 20×20 array (I find peaks by performing "imregionalmax" over each 20×20 arrays). As the dimension of my array represents, I have 51 of these 20×20 arrays. I want to do this without using for loop to iterate over all these 51 arrays and also, increase my speed. Can anyone help me with this?
UPDATE
This is my original time-consuming part of code:
z = 0;
for i=1:size(AF_Nuni,1),
peaks = sort(AF_Nuni(i,imregionalmax(squeeze(AF_Nuni(i,:,:)))));
peaksi = peaks(end - 1);
z = z + peaksi;
end
"AF_Nuni" is a 1156*34*34 gpuArray. it takes about 8.2 seconds which is not acceptable for me.
12 件のコメント
回答 (2 件)
Matt J
2024 年 1 月 13 日
編集済み: Matt J
2024 年 1 月 14 日
Perhaps as follows,
AF_Nuni = permute(AF_Nuni,[2,3,1]); %Avoid this permutation by forming AF_Nuni
%in an appropriate order
AF_Nuni( ~imregionalmaxSices(AF_Nuni) ) = nan;
peaks=maxk( reshape( AF_nuni, [],size(AF_Nuni,3) ) ,2,1);
z=sum(peaks(2,:));
function D=imregionalmaxSices(A)
B=padarray(A,[1,1],-inf); %A is the input array
C=reshape( imregionalmax(B(:,:)) ,size(B));
D=C(2:end-1,2:end-1,:); %the result
end
9 件のコメント
Image Analyst
2024 年 1 月 13 日
peaks = sort(AF_Nuni(i,imregionalmax(squeeze(AF_Nuni(i,:,:)))));
The way you're indexing could be slowing you down. Normally you loop over the last index, not the first one. If you could rotate your volumetric image in advance and then iterate over the last index. You could access memory much more efficiently and you would not need to use squeeze.
2 件のコメント
Image Analyst
2024 年 1 月 14 日
Instead of
for i=1:size(AF_Nuni,1),
you need
for i=1:size(AF_Nuni,3)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!