How can I vectorise a nested for loop that uses a function?

3 views (last 30 days)
So I have a function that takes 3 inputs, a 2D array and the row and column position of an element within that array. It outputs a new 2D array with the specified element updated. I need to write a script that uses this function to perform calculations on every element within the array.
Right now my code looks like this:
for i = 1:size(array,1)
for j = 1:size(array,2)
newarray(i,j) = MyFunction(array,i,j)
end
end
Now this works perfectly fine, but I was wondering if it is possible to do this without the loops to increase performance and how would I do this?
  1 Comment
Cedric
Cedric on 4 Sep 2015
There is no better way of doing this than to make the function able to treat vector/array inputs. What is this function doing?

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!