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
>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
What about:
N = sqrt(sum(abs(MATRIX).^2,2))
hth
Jos
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
>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
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
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
TIMEIT is an excellent benchmarking function, freely
available on the File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile
.do?objectId=18798
Jos