Help me to optimise or vectorize the code

I need to optimize this snippet of code where siz is close to 300. tried vectorization with NDGRID but it takes too much memory and matlab throws an error
finalMat = double(zeros(siz,siz,siz));
for i = 1:size(point3d,1)
for j = 1:size(point3d,2)
finalMat(point3d(i,j,1),point3d(i,j,2),point3d(i,j,3)) = max(value(i,j),finalMat(point3d(i,j,1),point3d(i,j,2),point3d(i,j,3)));
end
end

댓글 수: 4

Adam
Adam 2014년 8월 28일
Isn't that code just storing value(i,j) if it is positive and 0 otherwise or am I missing something?
Ank
Ank 2014년 8월 28일
No basically I am converting the 3d coordinate stored in point3d into to actual 3d array (finalMat). the index might repeat so taking the maximum value of all such occurance.
Joseph Cheng
Joseph Cheng 2014년 8월 28일
편집: Joseph Cheng 2014년 8월 28일
I'm curious that it takes too much memory (edit: just re-read and realized siz is not apply to size of point3d. What is the size of point3d?) and what the error is. What is the error it gives? is point3d contain non-integer numbers?
point3d = imread('pears.png');
value = rgb2gray(point3d);
siz = 300;
finalMat = double(zeros(siz,siz,siz));
tic
for i = 1:size(point3d,1)
for j = 1:size(point3d,2)
finalMat(point3d(i,j,1),point3d(i,j,2),point3d(i,j,3)) = max(value(i,j),finalMat(point3d(i,j,1),point3d(i,j,2),point3d(i,j,3)));
end
end
toc
I tried the above code as a vague try to experiment with it and it goes quickly and doesn't throw an error.
Ank
Ank 2014년 8월 28일
Currently it gets done in 100 milliseconds but i was looking for it to be faster. Size of point3d is is like 2000x2000.

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

답변 (1개)

Guillaume
Guillaume 2014년 8월 28일

0 개 추천

You could try using accumarray, but I'm not sure it'd be any faster:
rowsubs = point3d(:, :, 1);
colsubs = point3d(:, :, 2);
pagsubs = point3d(:, :, 3);
finalMat = accumarray({rowsubs(:) colsubs(:) pagsubs(:)}, value(:), [siz siz siz], @max);
If you can modify your algorithm to store your point3d coordinates and your values as vectors, you could dispense with all the (:) in accumarray.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

Ank
2014년 8월 28일

댓글:

Ank
2014년 8월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by