How to vectorize the following code snippet?

1 view (last 30 days)
x = -1.0:0.2:1.0;
y = -2:0.25:3;
length_x = length(x);
length_y = length(y);
for nx=1:length_x
for ny=1:length_y
result(nx, ny) = exp(x(nx)^2-y(ny)^2);
end
end

Accepted Answer

KSSV
KSSV on 15 Feb 2021
x = -1.0:0.2:1.0;
y = -2:0.25:3;
[X,Y] = meshgrid(x,y) ;
result = exp(X.^2-Y.^2);

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!