I have a function F that receives an 2D index (i and j) and a constant array C. I want to run this function on each cell in a matrix, meaning – in the (i,j) cell I’ll have the value F(i,j,C).
Is there a way to do it in matlab without using loops?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To apply a function to every item in an array or matrix, use
arrayfun.To use arrayfun, you need to modify the signature of your function
Fso that it takes the item in your matrix at (i,j), not the indicies themselves.Also, a purely vectorised solution is often faster than using
arrayfun. For example, compare