P = [1 2 3]
And a matrix
Q = [ 1 2 3
3 4 5
6 7 8]
I want to multiply each element of Q by the corresponding column
element of P
For example, from above I would get
Q1 = [1 2 3
6 8 10
18 21 24]
The only catch is I was hoping not to use a loop or create a larger
matrix using repmat(p,1,3)
Thanks for your help,
D
Thanks again for your help
D
i think that you wont be able to do it with a matrix multiplication
because if the first matrix has size [nxm] and the second [oxp] the
result will be [nxp].... and for matrix multiplication to exist you
also need m=o.... so if you want the result matrix to have [3x3] you
will need n = 3 and p=3.... the fact m = o will force you to
increase p length...
using element by element matrix multiplication you will also need to
make both sizes of multiplying matrices equal to the size of the
result matrix....
if anyone has a more creative way of doing what is proposed i would
be glad to know about it....
see you
This isn't increasing the size of P. P is unchanged after
this operation.
- Randy
Thanks for your help,
Demetri
You're correct that a temporary large matrix is created. If N and
M are very large, you could run into a memory-full error and
the command wouldn't execute. In that case you'd need another
approach, like doing it a column at a time. But if memory is
sufficient to create the temporary matrix, the operation should
be fast. Matlab is pretty well optimized.
> Specifically, I was wondering how efficient this would be. I am
> trying to find the fastest way to perform this operation.
If there is sufficient PHYSICAL memory, using built-in matrix
operations
is fastest. If virtual memory and disk swaps are required, that will
slow things down.
- Randy
Having gone through this before and not knowing how big the matrices
are going to be: I would suggest writing a mex function. I usually
have to element wise muliply each row of a matrix by a row vector or
each column by a column vector.
It was happening enough and the size of the matrices varied from very
small to very large (where the usually repmat, etc tricks took too
much memory) that I was forced to write a generic mex function to
handle these situations.
These are the times where I wish Matlab worked similar to R
(www.r-project.org). R knows to automatically "reuse" the row vector
each time.
If I had to pick two features to bring from R to Matlab it would be
the data "reuse" feature and how R allows for arguements such as
myfunc(data = adefault, paramb = default2) so when you call the
function you can either: pass variables in any order and two omit
variables since the defaults are right their in the declaration.
Stephen
1) have a look at <doug schwarz>'s toolbox available here
<http://www.frontiernet.net/~dmschwarz/genops.html>
2) another one of the many solutions
function a=foo(a,op,b)
% note: input check is removed for sake of brevity
sop='+-*/';
mop={@plus,@minus,@times,@rdivide};
op=mop{sop==op};
for i=1:size(a,2)
a(:,i)=op(a(:,i),b);
end
us
The best way to do this without a loop and without
some variant of repmat or a mex function is with a
sparse diagonal matrix multiply.
P = [1 2 3]
Q = [ 1 2 3
3 4 5
6 7 8]
n = length(P);
Q1 = spdiags(P',0,n,n)*Q
Q1 =
1 2 3
6 8 10
18 21 24
This should be efficient both in time and memory,
although it would probably not be as efficient as a
mex function. It should be quite reasonable though.
HTH,
John D'Errico
--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945
Those who can't laugh at themselves leave the job to others.
Anonymous