1

Let's say I have a function f(vec) which takes as input a vector (array) of size 3. I have a matrix M of size 100 x 100 x 3. Is there a way I can somehow apply f to every cell of M, outputting a matrix N of size 100 x 100? For example, N(10,20) would equal

f( M(10,20,1), M(10,20,2), M(10,20,3) ) 

Obviously I could use a for loop, but I feel like this should be vectorizable.

0

2 Answers 2

3

Vectorization in this context means rewriting the function in a way that it accepts multiple inputs at once. Assuming that this is not possible, a for loop is the best possibility.

Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way to do this is arrayfun

arrayfun(@(x,y,z) f(x,y,z), M(10,20,1), M(10,20,2), M(10,20,3) , 'uni', 0) 

1 Comment

Note that using arrayfun isn't vectorisation, it's basically equivalent to using a for loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.