MATLAB: Check if element in array are square of each other

nested loop

Hello,
I have a problem where I have to find possible squares in an array. For example [7 5 49] or [49 5 7] is true since 7 squared is 49, but [11 13 25] should return false.
Is there a way to do it better than a nested loop?
Thank you!
function y = isItSquared(x)
y = false;
for i = 1:length(x)
for j = i+1:length(x)
if x(i)^2 == x(j) || x(i) == x(j)^2
y = true
break
end
end
end

Best Answer

You can get rid of one of the for loops:
function y = isItSquared(x)
y = false;
xSquare = x.^2;
for ii = 1:length(x)
if (sum(x(ii) == xSquare) ~= 0)
y = true;
return
end
end