1

Is there any simple way to apply a function Foo to multiple arrays (matrices) A,B,C, besides creating a bigger array?

Essentially I want something similar to "map" in Python, like:

L = [A, B, C]
map(foo, L)

Let's assume to cases, 1. the function Foo is applied component-wise, does not change the dimension of the matrix, for example, log(), exp(). 2. Function is a general function,working on the whole matrix, for example, removerows().

szli
  • 36,893
  • 11
  • 32
  • 40
  • 1
    See my answer to the linked question. The `cellfun` function may do what you want. – kenm Mar 02 '13 at 17:04

2 Answers2

1

There is no built in analogue to 'map' in MATLAB by default. However, you could always write the functionality on your own; that has been done here. Put in in your MATLAB bin and you should be ready to go.

If you want your code to be portable across systems though, I'd recommend simple looping.

Community
  • 1
  • 1
Roney Michael
  • 3,964
  • 5
  • 30
  • 45
0

You can use varargin and varargout to create a function that takes an arbitrary number of inputs, and produces an arbitrary number of outputs.

So then you would be able to write:

[D, E, F] = map(foo, A, B, C);
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680