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: Multiply each column vector with another column vector - comp.soft ...Hello, I was wondering if there was a method to multiply each column vector of a 2d-array, with another column vector without using a loop. For ex... Vector/Matrix Multiplication - comp.soft-sys.matlabMultiply each column vector with another column vector - comp.soft ... Vector/Matrix Multiplication - comp.soft-sys.matlab... Vector/Matrix Multiplication - comp.soft-sys ... Splitting data into two columns - comp.lang.awkvector splitting to many vectors - comp.soft-sys.matlab multiply columns by vector - comp.soft-sys ... Splitting data in Matlab - comp.soft-sys.matlab vector ... vector splitting to many vectors - comp.soft-sys.matlabMultiply each column vector with another column vector - comp.soft ... vector splitting to many vectors - comp.soft-sys.matlab > > > > For example, I want to split a n x m ... Create a vector out of other two vectors - comp.soft-sys.matlab ...Multiply each column vector with another column vector - comp.soft ..... 12 14 16 18 I am sure there are many other ... each row vector of one matrix by each column ... Can mat2cell return a cell array of columns? - comp.soft-sys ...... soft-sys.matlab... would satisfy a large number of purposes, and working with the cell array result from mat2cell ... comp.soft-sys.matlab Multiply each column vector ... autocorrelation command xcorr - comp.soft-sys.matlabI can see that rects' is a row vector (1xN), but if you are multiplying it by y a Mx1 column vector, than you're getting a MxN matrix as an input to xcorr() and that can ... matrix multiplication - comp.graphics.api.openglMultiply each column vector with another column vector - comp.soft ... multiply matrix with vector - comp.soft-sys.matlab Vector/Matrix Multiplication - comp.soft-sys ... simulink matrix - comp.soft-sys.matlabI have a column vector x and I want to creat another matrix from ... C6713 - comp.soft-sys.matlab simulink ... Numerically multiply and divide any number of scalar, vector ... How to repeat a vector to creat a matrix - comp.soft-sys.matlab ...How to increment vector/array?? - comp.soft-sys.matlab One column has values ranging from 0-n ... multiply matrix with vector - comp.soft-sys.matlab How to repeat a vector ... How to create a unit step function?? - comp.soft-sys.matlab ...... end)=1; this gives me a column with the first five rows being 0 and the rest 1 i thought i could multiply ... rows as samples in the time vector T, and as many columns ... cross product between vector and array - comp.soft-sys.matlab ...Multiply each column vector with another column vector - comp.soft ... cross product between vector and array - comp.soft-sys.matlab ... Do you mean something like this ... different length vectors -> one matrix - comp.soft-sys.matlab ...The values of the vector are somewhat similar to the ones in the matrix, and what I w... ... And this for each of the 36 different columns. However, since there are different ... Order of operations in a vectorized function - comp.soft-sys ...As for the use of .* as opposed to multiplying elements one at a time, there should ... you wrote X = A*X; where A is an n by n matrix and X is an n by 1 column vector ... Implicit method of a PDE using L.U multiplication - comp.soft-sys ...> %the M+1 is the number of columns. > %To create a row or a column vector we set the appropriate argument of zeros to one. > alpha=dt/(dx)^2; > alpha2=alpha^2 ... Multiply each column vector with another column vector - comp.soft ...Hello, I was wondering if there was a method to multiply each column vector of a 2d-array, with another column vector without using a loop. For ex... Multiplying matrices and vectors - Math InsightMatrix-vector product. To define multiplication between a matrix $A$ and a vector $\vc{x}$ (i.e., the matrix-vector product), we need to view the vector as a column ... 7/26/2012 9:05:15 AM
|