سؤال

essentially I have a rotation matrix like so:

% rot    = [  cos(theta)    sin(theta)  0;
%            -sin(theta)    cos(theta)  0;
%            0              0           1];

except that theta is 1xN, so I create the following monster:

rot = zeros(3,3,SIZE);
rot(1,1,:) = cos(theta);
rot(1,2,:) = sin(theta);
rot(2,1,:) = -sin(theta);
rot(2,2,:) = cos(theta);
rot(3,3,:) = ones(1,SIZE);

Now, I have a 1x3xN matrix for which I need to right-matrix-multiply each 1x3 column vector with its corresponding 3x3 rotation matrix along the dimension of length N.

I have some notion that this might be possible with bsxfun? But I have not had success in figuring it out...

هل كانت مفيدة؟

المحلول

So you have one matrix R of size=[3 3 N], and you want to multiply a 1x3xN vector V (presumably one theta corresponds to one page of V). This means you want the sum over the second dimension. Solution

result = squeeze(sum(bsxfun(@times, R, V), 2));

First multiply elements (bsxfun takes care of the magical expansion along the first dimension), then sum over the second dimension (which is what a matrix multiplication would do), then squeeze out the singleton second dimension to give a 3xN result.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top