Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

norm of every row of a matrix

564 views
Skip to first unread message

David

unread,
Jul 6, 2008, 8:46:02 PM7/6/08
to
I have a matrix like this (each row is a vector):

1 2 3
4 5 6
7 8 9
...


I would like to get back
3.7
8.7
13.9
...
(the norm of each row as a vector)

Is there a way to do this without a for loop? I didn't see
anything that looked promising in doc norm...

Thanks!

Dave

Walter Roberson

unread,
Jul 6, 2008, 8:58:13 PM7/6/08
to
In article <g4rp0a$aug$1...@fred.mathworks.com>,

David <david...@gmail.com> wrote:
>I have a matrix like this (each row is a vector):

>1 2 3
>4 5 6
>7 8 9

>...
>I would like to get back
>3.7
>8.7
>13.9
>...
>(the norm of each row as a vector)

>Is there a way to do this without a for loop?

arrayfun(@(idx) norm(TheMatrix(idx,:)), 1:size(TheMatrix,1));
--
"He wove a great web of knowledge, linking everything together,
and sat modestly at a switchboard at the center, eager to help."
-- Walter Kerr

Jos

unread,
Jul 7, 2008, 1:34:01 AM7/7/08
to
"David " <david...@gmail.com> wrote in message
<g4rp0a$aug$1...@fred.mathworks.com>...

What about:

N = sqrt(sum(abs(MATRIX).^2,2))

hth
Jos

Nitin Chhabra

unread,
Jul 7, 2008, 2:15:04 AM7/7/08
to
"Jos " <DEL...@jasenDEL.nl> wrote in message <g4s9s9
$5np$1...@fred.mathworks.com>...

Hi,

I think Jos answer is more appropriate answer as the
previous one was a form of loop only and the question
states without loop

with regards
Nitin

Walter Roberson

unread,
Jul 7, 2008, 3:58:02 AM7/7/08
to
In article <g4sc98$l3j$1...@fred.mathworks.com>,
Nitin Chhabra <nitin....@st.com> wrote:

>I think Jos answer is more appropriate answer as the
>previous one was a form of loop only and the question
>states without loop

Nope, the "previous one" was

arrayfun(@(idx) norm(TheMatrix(idx,:)), 1:size(TheMatrix,1));

which has no loop, only a function call with
two arguments, one being an anonymous function handle
and the other being a simple vector of [1 2 3 4 ... ]
up to the number of rows in TheMatrix.


> > N = sqrt(sum(abs(MATRIX).^2,2))

I haven't checked the math behind norm (I don't recall seeing
norm defined in terms of svd before).

I don't see at the moment why one would want to take abs(MATRIX) before
squaring it, unless there is some thought that MATRIX might contain
complex numbers ? But for real numbers, abs() or not would just be an
optimization. Possibly of more interest {possibly not} is that this is
the 2 norm specifically, and is not immediately adaptable to the 1 norm
or infinity norm, or the Frobenius norm, whereas the arrayfun solution
is immediately adaptable by just adding the appropriate option to the
norm() call.

--
"All is vanity." -- Ecclesiastes

John D'Errico

unread,
Jul 7, 2008, 5:36:02 AM7/7/08
to
robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message
<g4siaa$flk$1...@canopus.cc.umanitoba.ca>...

Walter, while your solution does solve the
problem, it does so somewhat poorly.

M = rand(1000,10);

timeit(@() arrayfun(@(idx) norm(M(idx,:)), 1:size(M,1)))
ans =
0.056981

timeit(@() sqrt(sum(abs(M).^2,2)))
ans =
0.001092

timeit(@() sqrt(sum(M.^2,2)))
ans =
0.0010544

On this rather simple problem, the arrayfun
solution takes 50 times as long to evaluate.
Use of arrayfun here is like using a Mack
truck to carry a pea to Boston.

Finally, note that the use of abs or not is
roughly only an additional 5% in the time
required.

John

Jos

unread,
Jul 7, 2008, 6:35:03 AM7/7/08
to
robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g4siaa$flk$1...@canopus.cc.umanitoba.ca>...
> In article <g4sc98$l3j$1...@fred.mathworks.com>,
> Nitin Chhabra <nitin....@st.com> wrote:
>
> >I think Jos answer is more appropriate answer as the
> >previous one was a form of loop only and the question
> >states without loop
>
> Nope, the "previous one" was
>
> arrayfun(@(idx) norm(TheMatrix(idx,:)), 1:size(TheMatrix,1));
>
> which has no loop, only a function call with
> two arguments, one being an anonymous function handle
> and the other being a simple vector of [1 2 3 4 ... ]
> up to the number of rows in TheMatrix.
>
>
> > > N = sqrt(sum(abs(MATRIX).^2,2))
>
> I haven't checked the math behind norm (I don't recall seeing
> norm defined in terms of svd before).


Indeed, for the 2-norm ABS can be omitted. For odd valued
norms, ABS is required. The following ananymous function
will calculate the P-norm for each row of a matrix

rownorm = @(X,P) sum(abs(X).^P,2).^(1/P)

>
> I don't see at the moment why one would want to take
abs(MATRIX) before
> squaring it, unless there is some thought that MATRIX
might contain
> complex numbers ? But for real numbers, abs() or not would
just be an
> optimization. Possibly of more interest {possibly not} is
that this is
> the 2 norm specifically, and is not immediately adaptable
to the 1 norm
> or infinity norm, or the Frobenius norm, whereas the
arrayfun solution
> is immediately adaptable by just adding the appropriate
option to the
> norm() call.


For vectors, the Frobenius norm is equal to the 2-norm, and
the Inf norm is the absolute maximum.

Jos

ravi.ch...@gmail.com

unread,
Jul 7, 2008, 11:28:07 AM7/7/08
to
mynorm = (sqrt(sum((A').^2)))

David

unread,
Jul 7, 2008, 11:55:03 AM7/7/08
to
what is this "timeit" function? I dont seem to have it - is
it the same as doing tic; function(); toc ?

Jos

unread,
Jul 7, 2008, 12:29:02 PM7/7/08
to
"David " <david...@gmail.com> wrote in message <g4te8n$4q9
$1...@fred.mathworks.com>...

> what is this "timeit" function? I dont seem to have it -
is
> it the same as doing tic; function(); toc ?

TIMEIT is an excellent benchmarking function, freely
available on the File Exchange:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile
.do?objectId=18798

Jos

Harold Voepel

unread,
Apr 2, 2017, 1:35:06 PM4/2/17
to
"David Doria" wrote in message <g4rp0a$aug$1...@fred.mathworks.com>...
Hi Dave:

Why not use linear algebra? That's where Matlab shines.

For any nx3 matrix, M, say with row elements [x y z], the norm, d, of each row in R3, are given by the n-length vector d = sqrt(diag(M*M')). Of course this also works in any m-dimensional space using an nxm matrix.

Cheers, Hal

Lauren Schlenker

unread,
May 25, 2017, 5:00:14 PM5/25/17
to
"Harold Voepel" wrote in message <obrco7$aot$1...@newscl01ah.mathworks.com>...
This is awesome, I don't remember ever learning this in Lin. Algebra. I've googled this exact problem many times whenever it comes up, and this is the first time I've found an answer I'm really happy with that I can keep using. Thanks for sharing!!

Hazem

unread,
Jun 14, 2017, 2:54:08 AM6/14/17
to
"Lauren Schlenker" wrote in message <og7gkj$an3$1...@newscl01ah.mathworks.com>...
This is certainly the most elegant solution when it comes to code readability. But it is inefficient for huge matrices. Most computed products are not needed and discarded. I am using n=1000 and m=10000 in my application. The proposed linear algebra solution is about 5 times slower than the simple one presented earlier in the thread:
N = sqrt(sum(M.^2,2));
which only computes the needed squares, no more, no less.
0 new messages