Create a matrix with multiple values at each element and apply a function to all?
Show older comments
This is for optimization purposes - I have 2 linspaces of parameters, and a 2 parameter function f (random values/function):
Xs = linspace(0, 1000, 1000)
Ys = linspace(0, 10000, 10000)
f = @(x, y) cos(x) + sin(y)
I want to populate a matrix M which has size [length(Xs), length(Ys)], where each element of M is f(x, y) for each x in Xs and y in Ys.
I can do this slowly with a for/parfor loop - how can I use direct vectorisation to achieve this?
Thanks
Accepted Answer
More Answers (1)
the cyclist
on 9 Apr 2020
Here is one way:
[XXs,YYs] = meshgrid(Xs,Ys);
fxy = f(XXs,YYs);
Categories
Find more on Creating and Concatenating Matrices 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!