<p>I want to apply a function to all columns in a matrix with MATLAB. For example, I'd like to be able to call smooth on every column of a matrix, instead of having smooth treat the matrix as a vector (which is the default behaviour if you call <code>smooth(matrix)</code>).</p>

<p>I'm sure there must be a more idiomatic way to do this, but I can't find it, so I've defined a <code>map_column</code> function:</p>

<pre><code>function result = map_column(m, func)
    result = m;
    for col = 1:size(m,2)
        result(:,col) = func(m(:,col));
    end
end
</code></pre>

<p>which I can call with:</p>

<pre><code>smoothed = map_column(input, @(c) (smooth(c, 9)));
</code></pre>

<p>Is there anything wrong with this code? How could I improve it?</p>

If this is a common use-case for your function, it would perhaps be a good idea to make the function iterate through the columns automatically if the input is not a vector.
This doesn't exactly solve your problem but it would simplify the functions' usage. In that case, the output should be a matrix, too.
You can also transform the matrix to one long column by using
```
m(:,:) = m(:)
```
. However, it depends on your function if this would make sense.