MATLAB: Apply the same function into several columns

data columns

Hi!
I have a matrix A (50×50), and every column of the 50 are different subjects. I want to apply the same function to each column separetely and save the answers of the function into a different matrix B(50×1) The function is exactly the same, all that changes is the data on which the function is performed on.
How can I do it?
Thanks!

Best Answer

Use a loop:
B = zeros(1, size(A, 2));
for column = 1 : size(A, 2)
B(column) = yourfunction(A(:, column));
end
Or you can use cellfun, which is just a loop in disguise:
B = cellfun(@yourfunction, num2cell(A, 1));
edit: That's assuming the function cannot already operate on the columns of the whole matrix, as pointed out by Star.