user5045533
user5045533

Reputation:

Error while trying to calculate power in matlab

I have this function in matlab-

function [c,arr2]=dist1(i,c,arr1,arr2,A,mx,point)      
    for j=i+1:mx
        if arr1(i,j)==1 & A(j)~=0
            x1=point(i,1);
            y1=point(i,2);
            x2=point(j,1);
            y2=point(j,2);
            d=((((x1-x2).^2)+((y1-y2).^2)).^(0.5));
            if d< 0.5
                arr2(c)=i;
                c=c+1;
                [c,arr2]=dist1(j,c,arr1,arr2,A,mx,point);
            end
        end
    end
end

When I call this function this function I get following error-

Integers can only be raised to positive integral powers.

Error in dist1 (line 9)
            d=((((x1-x2).^2)+((y1-y2).^2)).^(0.5));

This works fine if I remove power of 0.5 in the calculation of d.Why am I getting this error,there seems to be nothing wrong in this statement.Also I checked the values of x1,x2,y1,y2 in the preceding lines and they are

x1=208 y1=171 x2=207 y2=162

Upvotes: 0

Views: 134

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112749

The error is pretty explicit:

Integers can only be raised to positive integral powers.

Your x1, x2, y1, y2 variables seem to be of an integer data type (such as uint8, int32, ...). They need to be double (or single) to perform that operation. So, try

d = double((((x1-x2).^2)+((y1-y2).^2)).^(0.5))^0.5;

Note also that, since x1, x2, y1, y2 are scalars, you could remove the dots:

d = double((((x1-x2)^2)+((y1-y2)^2))^0.5)^0.5;

Upvotes: 2

Related Questions