How to Vectorize this Code?
이전 댓글 표시
I have this 147000 x 3 array, where each index represents a 3D vector. I want to use the vector at each index to find the angle between it and a predefined vector "b0" [ 0 1 0], this results in a 147000 x 1 array where each index has a single angle. I have been able to do this by using a normal for loop, however I was wondering if there was a way to vectorize this so it will be faster. I have been trying, but it either says that it can't do the dot product because arrays are of incompatible size, or it will only calculate the angle of the first index, then copy that 147000 times into the Nx1 array.
Thanks for any help. 

댓글 수: 4
James Tursa
2022년 9월 21일
You can use a matrix multiply with b0 as a column vector to get the dot products. E.g.,
d = n3Array * b0(:); % the (:) syntax results in a column vector
Krishan Bansal
2022년 9월 21일
Matt J
2022년 9월 21일
@Krishan Bansal Please post your code and output as text, not images, to facilitate copy/pasting.
Krishan Bansal
2022년 9월 21일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

