piecewise matrix multiplication with vector

Hi,
I am trying to minimize a function s = b - Yr in a vectorized form. Y is a projection matrix and r the coordinates. I would like to multiply the first two rows of Y with the first vector in r and then the two second ones in y with the second one in r. Now this works:
b = zeros(2,1);
Y = [1 2 3 ; 0 0 1 ]
r = [0 0 1]';
s =
-3
-1
Now how could I extend this? Like this:
Y = [1 2 3 ; 0 0 1 ; 4 5 6 ; 0 0 1]
r = [0 0 1 ; 0 0 1]';
Such that I have the same form answer as in b variable? like b = zeros(2,90);
The variable Y i make like this:
angles = -88:2:90
for i= 1:size(angles,2)
if i== 1
Y = [cos(angles(1,i)) 0 sin(angles(1,i)); 0 1 0];
else
Y = vertcat(Y,[cos(angles(1,i)) 0 sin(angles(1,i)); 0 1 0]);
end
end
And in the minimization I try to find the z coordinate in r variable: r = [0 0 x]
Many thanks for any help!

 採用された回答

dpb
dpb 2015 年 1 月 11 日

1 投票

Sometimes it's just easiest to write a loop...
j=0;
for i=1:2:length(Y)
j=j+1;;
p(:,j)=Y(i:i+1,:)*r(:,j);
end
s=b-p; % objective function
With some thought this could be written w/ accumarray or the like but the above is straightforward and easy to debug. If it turns out the loop is a bottleneck verified by profiler, then I'd worry about it.

1 件のコメント

mb
mb 2015 年 1 月 11 日
thanks dpb! this will not be in the cost function so this will be perfect!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

mb
2015 年 1 月 11 日

コメント済み:

mb
2015 年 1 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by