Vectorization of matrices multiplication

조회 수: 2 (최근 30일)
Eugenio Grabovic
Eugenio Grabovic 2019년 1월 21일
댓글: Eugenio Grabovic 2019년 1월 21일
Hi,
i have some trouble finding out how to vectorize the following loop:
for k = 1:PointsLength
h(k,1) = transpose(PointsA(:,k) - PointsB(:,k))*normals(:,k);
end
The first thing that comes into my mind is doing this:
all the matrices have 3 x n dimensions and i want h to be 1 x n vector
k = 1:PointsLength
h(:) = transpose(PointsA(:,k) - PointsB(:,k))*normals(:,k);
but its obviously it's not the same thing since im trying to achieve row column multiplication only in pairs ( just k_th row times k_th column, without k_th row times other columns), and thus gives error cause he expects h to be n x n.
Any ideas? Thank you in advance.
  댓글 수: 2
madhan ravi
madhan ravi 2019년 1월 21일
Probably you want to use .* (element-wise multiplication) ?
Eugenio Grabovic
Eugenio Grabovic 2019년 1월 21일
k = 1:PointsLength
h(:) = transpose(PointsA(:,k) - PointsB(:,k)).*normals(:,k);
gives error cause he wants matrices to have the same dimensions (no transpose),
but by doing that i would only achieve (i_th,j_th) element multiplication only, with a 3 x n h matrix as a result (not what i was looking for).

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

채택된 답변

Jan
Jan 2019년 1월 21일
편집: Jan 2019년 1월 21일
h = dot(PointsA - PointsB, normals).'
or
h = sum((PointsA - PointsB) .* normals, 1).'
  댓글 수: 1
Eugenio Grabovic
Eugenio Grabovic 2019년 1월 21일
Oh wow, i feel so stupid... thank you very much !

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by