multiply columns by vector

  • Follow


Hello,
Say I had the matrix:

M= [A B ;
       C D ]

and the vector:

V = [a ;
       b ]

What would be the easiest way to get:

P=[Aa Ba;
     Cb Db]


I am currently doing:

P(:,1)=M(:,1).*V
P(:,2)=M(:,2).*V


The only way I can think of generalizing this to larger dimensions would be using loops.

Is there  simpler way, that would work even if M was 10 rows and 10 columns, and V was 10 columns ?

Thank you.
0
Reply Juliette 4/7/2010 7:03:06 PM

P = bsxfun(@times,M,V)
0
Reply Matt 4/7/2010 7:07:24 PM


"Matt Fig" <spamanon@yahoo.com> wrote in message <hpil5c$fal$1@fred.mathworks.com>...
> P = bsxfun(@times,M,V)

Perfect, thanks!
0
Reply Juliette 4/7/2010 7:12:23 PM

Anyone know why it's called bsx ?
0
Reply Juliette 4/7/2010 7:17:26 PM

Did you happen to look at the help?  The help is always a good place to start.
0
Reply Matt 4/7/2010 7:20:43 PM

Matt Fig wrote:
> P = bsxfun(@times,M,V)

Be careful:  Since V was specified as a column vector the answer should be

   P = bsxfun(@times,M,V.');

Something like

   P = bsxfun(@times,M,V(:).');

is more general if the orientation of V is not known.

-- 
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
0
Reply Doug 4/7/2010 8:04:04 PM

But Doug, by the example given:


M = magic(2)*pi;
V = [sqrt(2);sqrt(3)];

% Juliette's technique:
P(:,1)=M(:,1).*V;
P(:,2)=M(:,2).*V 

% BSXFUN
P2 = bsxfun(@times,M,V)


>>isequal(P,P2)
ans =
     1
0
Reply Matt 4/7/2010 8:15:43 PM

Matt is right,
the transpose would multiply the rows rather than the columns.


I did check the help: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/bsxfun.html

and couldn't find any clues as to why it's called bsxfun

Juliette
0
Reply Juliette 4/7/2010 8:37:04 PM

Look at the help from the MATLAB command window.  Notice the first line.

>> help bsxfun
  BSXFUN  Binary Singleton Expansion Function
    C = BSXFUN(FUNC,A,B) Apply an element-by-element binary operation to
    arrays A and B, with singleton expansion enabled. FUNC is a
    function handle. FUNC can either be a function handle for an 
    M-function, or one of the following built-in functions:

........ etc.,  etc., ......
0
Reply Matt 4/7/2010 8:40:37 PM

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message 
news:hpiqdg$8el$1@fred.mathworks.com...
> Matt is right,
> the transpose would multiply the rows rather than the columns.
>
>
> I did check the help: 
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/bsxfun.html
>
> and couldn't find any clues as to why it's called bsxfun

This is one case where the function help text has a bit more information 
than the function's documentation page.  HELP BSXFUN says:

> help bsxfun
  BSXFUN  Binary Singleton Expansion Function
*snip*

I suppose you could say that the first line of the help should be:

  BSXFUN  Binary Singleton eXpansion FUNction

but I think it's close enough.  Choosing names for new functions and new 
functionality can be (much) harder at times than people think, trust me.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ 


0
Reply Steven 4/7/2010 8:50:14 PM

In article <hpip5f$iij$1@fred.mathworks.com>,
 "Matt Fig" <spamanon@yahoo.com> wrote:

> But Doug, by the example given:
> 
> 
> M = magic(2)*pi;
> V = [sqrt(2);sqrt(3)];
> 
> % Juliette's technique:
> P(:,1)=M(:,1).*V;
> P(:,2)=M(:,2).*V 
> 
> % BSXFUN
> P2 = bsxfun(@times,M,V)
> 
> 
> >>isequal(P,P2)
> ans =
>      1

You're right, of course -- I must be getting old.  :-(

-- 
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
0
Reply Doug 4/7/2010 10:14:27 PM

Thanks Matt and Steve,
I always look up command documentation on the MW website rather than typing 
the help command , because I find that it's incredibly faster. When I type: 
help bsxfun

the command window says "busy" for about 25 seconds before the help page comes up.
And the website has more functionality.

but in the future I know where to look if I'm wondering the etymology of a command!
0
Reply Juliette 4/7/2010 10:16:06 PM

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message <hpikta$bjd$1@fred.mathworks.com>...

> 
> I am currently doing:
> 
> P(:,1)=M(:,1).*V
> P(:,2)=M(:,2).*V
> 
> 
> The only way I can think of generalizing this to larger dimensions would be using loops.
===============

Note, that for-loops have been found to be more optimal than bsxfun when one the dimensions of M is very small. For example, if M is 10000x10 and 
V is 10000x1 it would probably be better to loop over the 10 columns of M as you've proposed. Similarly, if there are a small number of rows, you would loop over those. 

Here is a function which I use in situations like these (and which I was forced to use before bsxfun came along). It analyzes the optimum looping strategy based on the dimensions of M:

function M=matmvec(M,v)
%"Matrix times vector". Multiplies a vector into columns or rows of matrix. 
%
% M_out=mattvec(M,v)
%
%The input M is a matrix.
%
%If v is a row vector, it will be multiplied into every row of M.
%
%If v is a column vector it will be multiplied into every column of M.
%
%It might be faster to do M*diag(v) or similar, but this route
%conserves more memory. 


[Mrows,Mcols]=size(M);
vlen=length(v);
mindim=min([Mrows,Mcols]);


    if size(v,2)==1 %v is a column vector

        
         if vlen~=Mrows; error 'Sizes not compatible'; end
         if Mcols==mindim, %loop over columns
                        
           for ii=1:Mcols
               M(:,ii)=M(:,ii).*v;
           end
          
         else %loop over rows
             
               for ii=1:vlen
                M(ii,:)=M(ii,:).*v(ii);
               end
             
         end
          

    else  %v is a row vector

         if vlen~=Mcols; error 'Sizes not compatible'; end

         if Mcols==mindim %loop over columns
         
              for ii=1:Mcols
                  M(:,ii)=M(:,ii).*v(ii);
              end
              
         else %loop over rows
             
               for ii=1:Mrows
                M(ii,:)=M(ii,:).*v;
               end
             
         end

    end
0
Reply Matt 4/7/2010 11:23:38 PM

12 Replies
239 Views

(page loaded in 0.003 seconds)

Similiar Articles:


















7/26/2012 9:05:15 AM


Reply: