How to apply a function to all rows in a matrix?

Consider, for example, the norm function. I have a matrix, and I want to apply the "norm" to each row in the matrix and get the vector of all the norms for each row in this matrix.

I was hoping I could make the norm (A, 'rows'), but that is not possible. Is there any other way to do this?

+3
source share
2 answers

You can do this without converting to an array of cells:

arrayfun(@(n) norm(A(n,:)), 1:size(A,1))
+8
source

Like this?

M = 1e4;
N = 1e3;
A = randn(M, N);

% Solve
B = mat2cell(A, ones(M, 1), N);
b = cellfun(@norm, B);

Maybe you can use arrayfun instead?

+1
source

All Articles