**Multiplying each matrix column by a column vector**I remember using a function that multiplied each column (or optionally row) of a matrix by a specific column (or optionally row) vector. It seems impossible to find the function in the MATLAB help since I don't remember the name. Anyone know which one I'm talking about?
Thanks,
Bryan
I don't know of such a particular function; however, for any matrix A and column vector v, it would seem straightforward to accomplish your task with:
A.*repmat(v, 1, size(A,2))
There's probably a more elegant/faster way that avoids using repmat, but I forget at the moment how to index matrices...

**Replace ones in a column of a logical matrix with element in the same column of another vector**Hi
I have a vector :
a = [1 2 3 4];
and a logical matrix :
b = [ 1 0 1 1;
0 1 0 1;
0 0 1 1;
1 1 0 1];
I want to get this result :
c = [1 0 3 4;
0 2 0 4;
0 0 3 4;
1 2 0 4];
I know that I can do this by repmat(a,4,1).*b but I am looking for the faster way to do this. is there any ?
regards
"Siavash Shakeri" wrote in message <itd5tc$d8i$1@newscl01ah.mathworks.com>...
> Hi
>
> I have a vector :
>
> a = [1 2 3 4];
>
> and a logical matrix :
>
> b = [ 1 0 1 1;
> 0 1 0 1;
> 0 0 1 ...

**multiply each column matrix by an element**I have a matrix A (say of size n x m) and I want to multiply each column of
the matrix by w(1), w(2),... w(m), respectively.
say, A = [A1 A2 .... Am] where A1, A2, ... Am are column vectors.
I want the result B = [w(1)*A1 w(2)*A2 .... w(m)*Am]
I can get this by B = A*diag(w).
Is there a faster method than this or is this the most efficient method?
(for small m,n there won't be any difference but if m,n are large creating a
diagonal matrix and multiplying might take time)
Thanks
Karthik
Karthik Ramanathan wrote:
> I have a matrix A (say of size n x m) and I want to multiply each ...

**Multiply a 3x3 matrix with each element of a vector**Hi.
I have a function that computes a 3x3 matrix. This function depends on one one variable which is a random angle.
This is the function:
function [Rrot]=rotate(rotationangle)
k=[0 0 1];
kx=[0 -k(1,3) k(1,2);k(1,3) 0 -k(1,1);-k(1,2) k(1,1) 0];
Rrot=eye(3,3)+(kx.*sin(rotationangle))+((1-cos(rotationangle))*kx^2);
%end of function
I have a vector called "theta" that contains 200.000 random angles in the range 0-2pi.
I want to call the function above for each of those angles and multiply the resulting rotation matrix with a vector, like this:
rotv=rotate(theta)*[1 0 0]&...

**multiplying, element wise, a row by each column of a matrix.**Any one can think of a better way to do this?
The problem: Given a row vector
v={v1,v2}
and matrix with same number of columns as v, such as
mat={{a11, a12},
{a21, a22},
{a31, a32}};
I need to multiply v1 by the first column, then
multiply v2 by the second column. (i.e. scale each column
of the matrix by the corresponding "weight" from the
row).
This results in
{{a11 v1, a12 v2},
{a21 v1, a22 v2},
{a31 v1, a32 v2}}
I wanted to find a 'smart' way or good command to do
it, but so far, had to do it the hard way. I'll s...

**how to multiply each element of a matrix by a number that depends on position?**hi;
Is there a trick in matlab to multiply each entry of a matrix
by a number that depends on the position of the entry, without
using a direct loop?
suppose I want to multiply matrix A by (-1)^(i+j) where i,j are
the entry position.
i.e., using a loop, the above is (M is number of rows, N number of cols)
for(i=1:M)
for(j=1:N)
B(i,j) = A(i,j) * (-1)^(i+j);
end
end
I wanted to see if there is a shorter way to do the above without
an explicit loop.
thanks,
Bill
Bill wrote:
>
>
> hi;
>
> Is there a trick in matlab to multiply each entry of a matrix
> by a numb...

**Get matrix element at position specified by a vector**-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello,
Let us consider you've got
a = [[2 2]; [3 4]]
a =
2 2
3 4
b = [1 1]
b =
1 1
How can I get the element of a specified by vector b, that is, in this
case, a(1,1)? Evidently, a(b) does not work (as expected).
Thanks.
- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --
It's the early bird that gets the worm.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourcef...

**Re: multiplying, element wise, a row by each column of a matrix.**In[6]:= v # & /@ mat
Out[6]= {{a11 v1, a12 v2}, {a21 v1, a22 v2}, {a31 v1, a32 v2}}
On Thu, Jun 21, 2012, at 05:19 AM, Nasser M. Abbasi wrote:
> Any one can think of a better way to do this?
>
> The problem: Given a row vector
>
> v={v1,v2}
>
> and matrix with same number of columns as v, such as
>
> mat={{a11, a12},
> {a21, a22},
> {a31, a32}};
>
> I need to multiply v1 by the first column, then
> multiply v2 by the second column. (i.e. scale each column
> of the matrix by the corresponding "we...

**Re: multiplying, element wise, a row by each column of a matrix. #2**Any one can think of a better way to do this?
The problem: Given a row vector
v={v1,v2}
and matrix with same number of columns as v, such as
mat={{a11, a12},
{a21, a22},
{a31, a32}};
I need to multiply v1 by the first column, then
multiply v2 by the second column. (i.e. scale each column
of the matrix by the corresponding "weight" from the
row).
This results in
{{a11 v1, a12 v2},
{a21 v1, a22 v2},
{a31 v1, a32 v2}}
I wanted to find a 'smart' way or good command to do
it, but so far, had to do it the hard way. I'll s...

**Convert nxn matrix to a column vector with (n^2) elements**Any ideas how to convert an nxn matrix to a column vector with n^2
elements?
Tara.Ann.Lorenz@gmail.com wrote:
> Any ideas how to convert an nxn matrix to a column vector with n^2
> elements?
Beware that Mathematica does not have any special construct to
distinguish row vectors from column vectors. A vector can be represented
by a one dimensional list made of numeric or/and symbolic elements.
Depending on the context and the operation in use, Mathematica
interprets the vector as a column or a row vector. (More generally,
vectors are tensors of rank 1.)
Having said...

**An easier functional way to divide each Column of matrix by a row vector, element-wise?**I have a list like this 'mat' and 'v' like this
mat = { {a1,a2},{b1,b2} }
v = {v1,v2}
I want to generate
mat={ {a1/v1, a2/v2}, { b1/v1, b2/v2 } }
I can't just do mat/v since this does
mat={ {a1/v1, a2/v1}, { b1/v2, b2/v2 } }
I solved this 2 ways, but I am still not happy.
I think there should be an easier way.
method 1 (not too natural)
-------------------------------
Clear["Global`*"]
mat={{a1,a2},{b1,b2}};
v={v1,v2};
Transpose[Transpose[mat]/v]
Out[93]= { {a1/v1, a2/v2}, {b1/v1, b2/v2} }
method 2 (too complicated)
----...

**Re: An easier functional way to divide each Column of matrix by a row vector, element-wise?**hello;
I found a better solution.
(after a strong coffee and staring on it for sometime)
method(5)
-----------
mat = {{a1,a2},{b1,b2}};
v = {v1,v2};
Inner[Divide,mat,v,List]
Out[61]= { {a1/v1, a2/v2}, {b1/v1,b2/v2} }
But I can't say though it was easy and intuitive to find
for me but at least the above solution is a functional and
I think the right Mathematica way of doing it. So I am
happy. Was good practice though.
--Nasser
On 6/27/2012 8:13 PM, Nasser M. Abbasi wrote:
> I have a list like this 'mat' and 'v' like this
>
> mat = { ...

**Re: An easier functional way to divide each Column of matrix by a row vector, element-wise? #2**#/v & /@ mat
Bob Hanlon
On Thu, Jun 28, 2012 at 4:02 AM, Nasser M. Abbasi <nma@12000.org> wrote:
> hello;
>
> I found a better solution.
>
> (after a strong coffee and staring on it for sometime)
>
> method(5)
> -----------
> mat == {{a1,a2},{b1,b2}};
> v == {v1,v2};
> Inner[Divide,mat,v,List]
>
> Out[61]== { {a1/v1, a2/v2}, {b1/v1,b2/v2} }
>
> But I can't say though it was easy and intuitive to find
> for me but at least the above solution is a functional and
> I think the right Mathematica way of do...

**Re: An easier functional way to divide each Column of matrix by a row vector, element-wise? #3**On Jun 28, 1:03 am, "Nasser M. Abbasi" <n...@12000.org> wrote:
> hello;
>
> I found a better solution.
>
> (after a strong coffee and staring on it for sometime)
>
> method(5)
> -----------
> mat = {{a1,a2},{b1,b2}};
> v = {v1,v2};
> Inner[Divide,mat,v,List]
>
> Out[61]= { {a1/v1, a2/v2}, {b1/v1,b2/v2} }
>
> But I can't say though it was easy and intuitive to find
> for me but at least the above solution is a functional and
> I think the right Mathematica way of doing it. So I am
> happy. Was good pra...

**Multiply each column vector with another column vector**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 example,
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> b = [2; 2; 2]
b =
2
2
2
>> for i = 1:3
A(:,i) = A(:,i) .* b;
end
>> A
A =
2 4 6
8 10 12
14 16 18
Is it possible to do this without a loop?
Thanks guys. Really trying to optimize my code.
On 10/21/2010 1:17 AM, Karthk wrote:
> Hello...

**help needed with multiplying each elements in one matrix with each element in another matrix**Hey, Some advice needed...
Say, for example, u have the following data: alpha = [0:15:360] and
say u have theta = [1x25]matrix. the problem i am having is this, I
am steeping through the matrices using a for loop,but i am not
getting all my values of alpha to multiply with each value of my
theta values.
So, for example, i am stepping through:
alpha = [0:15:360]
f=25;
for y=1:f
X1(y)=rand(1) + (i*rand(1));
Xt1(y)=rand(1) + (i*rand(1));
X2(y)=rand(1) + (i*rand(1));
Xt2(y)=rand(1) + (i*rand(1));
X0(y) = rand(1) + (i*rand(1));
Xt0(y)=rand(1) + (i...

**Multiply columns in a matrix by a column array**I have the following scenario:
x1 = [1;
2;
3;
4;
5];
x2 = [2 7;
3 8;
4 9;
5 10;
6 11];
x2 may vary in the number of columns, and the number of rows will be
large, but equal.
I would like to multiply the values in each row of x2 by the value in
the corresponding row of x1.
The desired result from the example would be:
2 7
6 16
12 27
20 40
50 55
What's the best way?
Typo: The last line of the result should be '30 55' not '50 55'
Christopher Brown wrote:
<Summary: He want to multiply each columin in a (for example) 5x2...

**matrix: multiply each element by each combination of all other element**This should be a simple problem but I haven't been able to figure out an efficient way to do it. If I have a matrix, or vector, and I want to multiply each element by every other element, and every combination of multiples of every other element, what is the best way to do it?
for ex: [1 2 3 4 5] would go to
[1, 2, 3, 4, 5, 2*3, 2*4, 2*5, 2*3*4, 2*4*5, 3*4*5, 4*5, 3*5, 3*4, 2*3*4*5]
Also, is there a simple way to test for and eliminate identical elements?
For example, [ 1 2 3 3 3 4 5] could go to [1 2 3 4 5] or [1 2 3 0 0 4 5].
I've tried messing around with multi-dimensional arr...

**adding column vector of a matrix to another matrix**Hi,
I have two matrices (3x4)
A= B=
1 5 6 6 5 4
4 7 9 1 2 1
2 3 0 3 2 7
1 2 1 0 1 1
I would like to add the last column vector of B to the matrix A, so as to obtain a 4x4 matrix.
How can I do that?
Thanks ALL in advance
On 7/6/2013 6:05 AM, m wrote:
> Hi,
>
> I have two matrices (3x4)
> A= B=
> 1 5 6 6 5 4
> 4 7 9 1 2 1
> 2 3 0 3 2 7
> 1 2 1 0 1 1
Those are 3x4, no...

**subtracting column vector from every column in matrix ...**how do i go about doing this?
do i need to replicate the column vector into
a matrix with duplicate column entries, and subtract
from the original matrix?
any help appreciated.
thanks,
old student wrote:
>
>
> how do i go about doing this?
> do i need to replicate the column vector into
> a matrix with duplicate column entries, and subtract
> from the original matrix?
> any help appreciated.
> thanks,
There's a function called repmat() that people often use. However,
all it's doing is this:
a=magic(5); % example matrix
b=(1:5)'; % example column
c=a-b(:,...

**add column matrix to each element of each diagonal in square matrix**A(mxm) square (where m is odd from 3 to 99)
E(1xn) (where n is the number of diagonals of A)
i need to add each element of E to each element of the corresponding diagonal of A
Result is B(mxm)
1st diagonal B(1,1)=A(1,1)+E(1,1);
2nd diagonal B(2,1)=A(2,1)+E(1,2); B(1,2)=A(1,2)+E(1,2);
3rd diagonal B(3,1)=A(3,1)+E(1,3); B(2,2)=A(2,2)+E(1,3); B(1,3)=A(1,3)+E(1,3);
and so on ...
nth diagonal B(m,m)=A(m,m)+E(1,n);
I kind of got stick below. Appreciate help with finishing this or a new solution.
For example when m=5;
n=9;
A=[1 2 3 4 5; 2 3 1 0 0; 1 1 2 3 5; 0 1 2 2 2; 3 3 1 1 ...

**How to multiply each element of one vector wirh all elements of another?**Hi,
I want to do the following:
a=[1 2 3];
b=a;
z= a <the wanted operation> b;
z should be
z = [1 2 3; 2 4 6; 3 6 9]
or abstract
z = [a(1)*b(1) a(1)*b(2) a(1)*b(3);
a(2)*b(1) a(2)*b(2) a(2)*b(3);
a(3)*b(1) a(3)*b(2) a(3)*b(3)]
I know, I could solve this with looping through elements of a and
them multiply them elementwise with b.
But is it also possible to avoid this looping and do it MATLAB
"native"?
regards
robert
In message ef1b578.-1@webx.raydaftYaTP, RobR burbled:
> I want to do the following:
>
> a=[1 2 3];
> b=a;
>
> z= a <the wante...

**multiply a 2D matrix with a 3D matrix and get a vector**Hi,
I have below matrices and I need a vector answer. But I don't know if this is possible or not! I'll appreciate any help.
A is 5x50
B is 3x5x50
I need Answer=A*B to be 3x1
Thanks,
April
"April Atoufi" <atoufi@gmail.com> wrote in message <k161ov$5fc$1@newscl01ah.mathworks.com>...
> Hi,
> I have below matrices and I need a vector answer. But I don't know if this is possible or not! I'll appreciate any help.
>
> A is 5x50
> B is 3x5x50
> I need Answer=A*B to be 3x1
>
> Thanks,
> April
You will need to be...

**adding column vector of a matrix to another matrix #2**Hi,
I have two matrices (3x4)
A= B=
1 5 6 6 5 4
4 7 9 1 2 1
2 3 0 3 2 7
1 2 1 0 1 1
I would like to add the last column vector of B to the matrix A, so as to obtain a 4x4 matrix.
How can I do that?
Thanks ALL in advance
result = [A B(:,end)]
...