square (x-y) in matlab

To square the difference between x and y in Matlab, you can use the following arithmetic operation:

main.m
result = (x - y)^2;
20 chars
2 lines

This will subtract y from x, and then square the result. The value will be stored in the variable result.

Alternatively, if you want to square each element of the variable (i.e., if x and y are vectors or matrices), you can use the . operator to perform element-wise squaring:

main.m
result = (x - y).^2;
21 chars
2 lines

This will subtract y from x element-wise, and then square each element.

gistlibby LogSnag