How to use "imregionalmax" without using for loop

조회 수: 1 (최근 30일)
moh mor
moh mor 2024년 1월 12일
편집: Matt J 2024년 1월 15일
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
Catalytic
Catalytic 2024년 1월 15일
Are the values of AF_Nuni non-negative?
moh mor
moh mor 2024년 1월 15일
yes it is nonnegative and something between 0 and 1.

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
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
moh mor
moh mor 2024년 1월 15일
unfortunatley, it gives the following error:
Error using movmax
Invalid data type. First input must be numeric or logical.
Matt J
Matt J 2024년 1월 15일
편집: Matt J 2024년 1월 15일
First input must be numeric or logical.
And indeed it should be! Surely if you are giving movmax non-numeric input, it is inadvertent, and something you can easily debug.

댓글을 달려면 로그인하십시오.


Image Analyst
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
moh mor
moh mor 2024년 1월 13일
편집: moh mor 2024년 1월 13일
thank you @Image Analyst,
Sorry , but I do not why when I reshape AF_Nuni to a 34*34*1156 and want to do whatever you said, I face with this error in the 1st iteration, i.e. i=1 (I have checked AF_Nuni demention and size already). can you help me with this?
peaks = sort(AF_Nuni(imregionalmax(AF_Nuni(:,:,i)),i));
here the error:
Error using gpuArray/subsref
Index exceeds matrix dimension.
Image Analyst
Image Analyst 2024년 1월 14일
Instead of
for i=1:size(AF_Nuni,1),
you need
for i=1:size(AF_Nuni,3)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by