Vectorize operations referencing adjacent elements of a matrix
1 次查看(过去 30 天)
显示 更早的评论
I have made the simple code:
for i=2:xsize-1
for j=2:ysize-1
for n=2:zsize-1
matrix(i,j,n)=matrix(i,j,n)+0.2*(matrix(i,j,n)*6-matrix(i-1,j,n)-matrix(i+1,j,n)-matrix(i,j-1,n)-matrix(i,j+1,n)-matrix(i,j,n-1)-matrix(i,j,n+1));
end
end
end
This is part of a diffusion problem and it is particularly slow because the 3D matrix is large. Is it possible to vectorize an operation like this? How do you vectorize references to the matrix like this?
0 个评论
采纳的回答
Andrei Bobrov
2014-2-18
编辑:Andrei Bobrov
2014-2-18
out = convn(matrix,cat(3,[0 0 0;0 -1 0;0 0 0],...
[0 -1 0;-1 1 -1;0 -1 0],...
[0 0 0;0 -1 0;0 0 0]),'valid');
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!