How to reduce the computation time for adding 3D-array?

Hi, I am trying to add multiple 3D-arrays to a bigger 3D-array at a specific index (x,y,z)
Below is the code, and it does work and compute the answer but it seems very ineffecient.
In this example, i only have 4 sets of coordiantes (x,y,z) but in real code, i have more than 1e6 sets of points.
It takes very long to compute the result with that many points.
Is there any way to reduce the computation time?
Thank you in advance
Regards
J
Big=zeros(500,500,500); %%%% Bigger Array
Small=rand(250,250,250);
x=[245; 220; 256; 270];
y=[245; 220; 256; 270];
z=[245; 220; 256; 270];
for n = 1 : length(x)
x_cord=x(n)-length(Small)/2;
y_cord=y(n)-length(Small)/2;
z_cord=z(n)-length(Small)/2;
x_end= x_cord + length(Small) -1 ;
y_end= y_cord + length(Small) -1;
z_end=z_cord + length(Small) -1;
if x_end <= length(Big)
Big(x_cord:x_end, y_cord:y_end,z_cord:z_end)=Big(x_cord:x_end, y_cord:y_end,z_cord:z_end)+Small();
end
end

댓글 수: 2

could also be done with accumarray, but I am not sure that would be faster considering the time to generate the coordinate matrices... though I did just think of a shortcut for that.
Hi Walter. Thank you for your comment.
What would be the coordinate matrices??
Does it mean x,y,z?
Thank you

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

 채택된 답변

Bruno Luong
Bruno Luong 2020년 12월 24일
편집: Bruno Luong 2020년 12월 24일
You can reformulate the loop as convolution of
A = accumarray([x(:) y(:) z(:)]-length(Small)/2,1,[500 500 500])
and
B = flip(flip(flip(Small,1),2),3)
(I left out the detail of overflowed for simplicity)
You might look at convn function. The problem I see is that A is sparse (1/125 in density) and might not be efficient as for loop.
You also might try this FEX to see if you can exploit the sparsity
Or this one using different method of compute convolution

댓글 수: 5

Thank you for your help.
Why do i need to perform the flip function for matrix B?
I also don't understand how accumarray function can add "Small" matrix values to a "Big" matrix at a specific points.
Convolution integral is defined in terms of integral of f(t)*g(x-t). The discrete analog to the x-t involves flipping an array. Where time needs to sort of go backwards for one of the functions in integration, you need to progress backwards in a matrix for the discrete version.
I tried the above method but it went into the infinite loop.
A = accumarray([x(:) y(:) z(:)]-length(Small)/2,1,[500 500 500])
B = flip(flip(flip(Small,1),2),3)
C=convn(A,B);
You might try the alternative convolution implementations in the links I post above.
As I said above, the alternative ways I have proposed might not be fater than your for-loop.

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

추가 답변 (1개)

Amrtanshu Raj
Amrtanshu Raj 2020년 12월 24일

0 개 추천

Hi,
You can use the parfor loop to use parallel processing and get higher computation speeds. However you will have to modify your for loop to be used for parfor loop.
Hope this helps !!

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2020년 12월 9일

댓글:

2021년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by