MATLAB Answers

How to loop over a function call?

1 view (last 30 days)
I want to evaluate a function Least_square(), over multiple iterations and return its sum. I am currently doing this by
fun = @(x) ( Least_square(x,y1) + Least_square(x,y2) + Least_square(x,y3) + Least_square(x,y4) )
Is there a better method to do this? Since I may want to evaluate it over multiple iterations
Thanks
  2 Comments
Avish Naredi
Avish Naredi on 20 Sep 2021
Hey, I have edited my question. I hope it is clear now. This function would be optimized on x, over different y values. So, by somehow I want to loop the calculations

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Sep 2021
Edited: Walter Roberson on 20 Sep 2021
y = [y1, y2, y3, y4]
fun = @(x) reshape(sum(arrayfun(@(Y) Least_square(x(:), Y), reshape(y, 1, [])),2), size(x))
  5 Comments
Avish Naredi
Avish Naredi on 21 Sep 2021
Yes, Thanks a lot. This is working

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!