Efficient code writing...
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi all. First time posting here, hope I can get some nice ideas. I have 3 large matrices (of equal size), representing the x,y and z coordinates of points. So say my matrices are 200 by 200, it means they are representing 200x200 points. Now, I want to apply this transformation matrix to each point, so that I get some linear combination of the point being transformed. Of course, I know I could opt for a loop where I update the indices of my matrices and each time apply the transformation. But I'd like to know how I can do this more efficiently... I know matlab works better using vectorisation, so I am thinking along the lines of using structures or something... Any ideas?
Thanks. Dan
채택된 답변
Andrei Bobrov
2011년 11월 16일
variant
C = cat(3,x, y, z);
s = size(C);
Cout = permute(reshape(M*reshape(permute(C,[3 2 1]),s(3),[]),s(3),s(2),[]),[3 2 1]);
variant 2
Cout2 = C;
for i1 = 1:size(C,2)
Cout2(:,i1,:) = (M*squeeze(C(:,i1,:)).').';
end
댓글 수: 0
추가 답변 (2개)
Sean de Wolski
2011년 11월 16일
A well written for-loop should be pretty fast. I would recommend doing that. make sure to preallocate your matrix of transformations to be equal to its final size.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!