MATLAB: Is better way to make this loop more effective

for loop

so, a have something like this:
x_a=[....]; %1xN
x_b=[....]; %1xM
for i=1:N
for j=1:M
c(i,j)= x_a(i)^2+x_b(j)^2 %or similar math operation using x_(i) and x_t(j)
end
end
Is a way to perform this operation more efficiently?

Best Answer

I would use the meshgrid function:
[X_a, X_b] = meshgrid(x_a, x_b);
c = X_a.^2+X_b.^2;
Note This is obviously UNTESTED CODE but it should work.