1

If i have the following matrix:

a=[10   1   0   1   1
   50   1   0   0   0
   60   0   0   0   1]

how can i multiply first column in the matrix [10 50 60]' as vector multiplication to the rest of the matrix and get the following:

b=[10  10  0  10  10
   50  50  0   0   0
   60  0   0   0   60]

3 Answers 3

5

For this, bsxfunis what you need:

b = [a(:,1) bsxfun(@times,a(:,2:end),a(:,1))]

hope this helps,

A.

1
  • As far as I'm concerned, this should be the accepted answer +1
    – Amro
    Feb 18, 2011 at 1:17
3

If you really want to frame it as a matrix multiplication, something like:

b = [a(:,1), diag(a(:,1))*a(:,2:end)]

should work (it's been a while since I've done any matlab, though).

1
  • Your answer seems to be the most straightforward one, firm connections to linear algebra as well. I skimmed the answers too hastily and wouldn't even posted my own one if I just had recognized your answers superiority at first sight.
    – eat
    Feb 17, 2011 at 23:06
2
firstcol = a(:,1);
b = [firstcol, a(:,2:end) .* repmat(firstcol,1,size(a,2)-1)];

(This really isn't the sort of thing we normally call vector multiplication or matrix multiplication though.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.