4

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

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

kloop
  • 4,537
  • 13
  • 42
  • 66

2 Answers2

9

You can do it without converting to a cell array:

arrayfun(@(n) norm(A(n,:)), 1:size(A,1))
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
1

Like this?

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

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

Maybe arrayfun could be used instead?