Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to "divide" a matrix by a vector?

Subject: How to "divide" a matrix by a vector?

From: Lawrence G

Date: 9 Mar, 2006 20:35:36

Message: 1 of 8

I'm brand new to MATLAB. I've searched hard for the answer to this
question in the online documentation, but I can't find it.

How do I do an element-by-element division of an matrix by a vector,
where each item in the vector specifies a scalar by which to divide
the corresponding row of the matrix? I.e., the first row of the matix
is divided by the first scalar in the vector, the second row of the
matix is divided by the second scalar in the vector, etc.

From what I can tell, the ./ operator requries the matrices to be the
same dimension.

I'm sure there must be an operator or some easy trick to accomplish
this.

Any help is appreciated.

Subject: How to

From: us

Date: 9 Mar, 2006 20:43:35

Message: 2 of 8

Lawrence G:
<SNIP gen op evergreen...

this hot beauty by <duane hanselman> was JUST released after an
interesting blog by <loren shure>

the blog

 <http://blogs.mathworks.com/loren/?p=23>

the function

 <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10295&objectType=file>

us

Subject: How to "divide" a matrix by a vector?

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 10 Mar, 2006 01:32:07

Message: 3 of 8

In article <ef2badd.-1@webx.raydaftYaTP>, "Lawrence G"
<questions.matlab@SPAMyaNOhNOoo.com> wrote:

> I'm brand new to MATLAB. I've searched hard for the answer to this
> question in the online documentation, but I can't find it.
>
> How do I do an element-by-element division of an matrix by a vector,
> where each item in the vector specifies a scalar by which to divide
> the corresponding row of the matrix? I.e., the first row of the matix
> is divided by the first scalar in the vector, the second row of the
> matix is divided by the second scalar in the vector, etc.
>
> From what I can tell, the ./ operator requries the matrices to be the
> same dimension.
>
> I'm sure there must be an operator or some easy trick to accomplish
> this.
>
> Any help is appreciated.
-----------------------
  You need to combine a 'repmat' operation with a './' operation. If M is
an n by m matrix and v is an n x 1 vector, then

 M./repmat(v,1,m)

will do it. This action allows both operands to be of the same size.

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford

Subject: How to

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 10 Mar, 2006 01:50:27

Message: 4 of 8

In article <ef2badd.0@webx.raydaftYaTP>, us <us@neurol.unizh.ch> wrote:

> Lawrence G:
> <SNIP gen op evergreen...
>
> this hot beauty by <duane hanselman> was JUST released after an
> interesting blog by <loren shure>
>
> the blog
>
> <http://blogs.mathworks.com/loren/?p=23>
>
> the function
>
>
<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10295&objectType=file>
>
> us
----------------------
  'dotdot' is a function that is sorely needed, Duane. Congratulations!
I would like to see MathWorks implement built-in operations along this
general line which do not require time-consuming duplication of array
elements. Maybe this will give them a prod in that direction.

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford

Subject: How to

From: Lawrence

Date: 9 Mar, 2006 21:12:30

Message: 5 of 8

Thanks to both of you. I've learned quite a bit.

Subject: How to

From: M. Tetouan

Date: 9 Mar, 2006 22:04:12

Message: 6 of 8

Lawrence G wrote:
>
>
> I'm brand new to MATLAB. I've searched hard for the answer to this
> question in the online documentation, but I can't find it.
>
> How do I do an element-by-element division of an matrix by a
> vector,
> where each item in the vector specifies a scalar by which to divide
> the corresponding row of the matrix? I.e., the first row of the
> matix
> is divided by the first scalar in the vector, the second row of the
> matix is divided by the second scalar in the vector, etc.
>
> From what I can tell, the ./ operator requries the matrices to be
> the
> same dimension.
>
> I'm sure there must be an operator or some easy trick to accomplish
> this.
>
> Any help is appreciated.

Given a matrix A of size mxn and a column vector v of size mx1, the
result of dividing the i-th row of A by the i-th element of v is
given by: result = inv(diag(v))*A

Example:
>> A = [4 8; 4 2];
>> v =[3;5]
>> diag(v)\A

Subject: How to

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 10 Mar, 2006 04:18:05

Message: 7 of 8

In article <ef2badd.4@webx.raydaftYaTP>, "M. Tetouan"
<centrino@linuxmail.org> wrote:

> Given a matrix A of size mxn and a column vector v of size mx1, the
> result of dividing the i-th row of A by the i-th element of v is
> given by: result = inv(diag(v))*A
>
> Example:
> >> A = [4 8; 4 2];
> >> v =[3;5]
> >> diag(v)\A
-------------------------------
 True, but that may not be an efficient way to obtain the desired result.
The overhead involved in replicating the vector v is surely much less than
that involved in solving the matrix equation between two n x n matrices
with the '\' operator. The computer would be wasting its time dealing
with mostly zeros in diag(v). It would be interesting to see some
actually time tests for large n on this question, but my own system is too
primitive to give reliable results.

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford

Subject: How to

From: M. Tetouan

Date: 10 Mar, 2006 08:56:12

Message: 8 of 8

Roger Stafford wrote:
>
>
> In article <ef2badd.4@webx.raydaftYaTP>, "M. Tetouan"
> <centrino@linuxmail.org> wrote:
>
>> Given a matrix A of size mxn and a column vector v of size mx1,
> the
>> result of dividing the i-th row of A by the i-th element of v
is
>> given by: result = inv(diag(v))*A
>>
>> Example:
>> >> A = [4 8; 4 2];
>> >> v =[3;5]
>> >> diag(v)\A
> -------------------------------
> True, but that may not be an efficient way to obtain the desired
> result.
> The overhead involved in replicating the vector v is surely much
> less than
> that involved in solving the matrix equation between two n x n
> matrices
> with the '\' operator. The computer would be wasting its time
> dealing
> with mostly zeros in diag(v). It would be interesting to see some
> actually time tests for large n on this question, but my own system
> is too
> primitive to give reliable results.
>
> (Remove "xyzzy" and ".invalid" to send me email.)
> Roger Stafford
>

repmat is not efficient for large matrices:

>> A = delsq(numgrid('A',450)); %105508x105508 matrix
>> v = sparse(rand(length(A),1));
>> result = diag(v)\A;
>> repmat(v,1,length(A))
??? Out of memory. Type HELP MEMORY for your options.

Error in ==> repmat at 62
        B = A(:, ones(siz(2), 1));

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us