简体   繁体   English

尝试在MATLAB中计算功效时出错

[英]Error while trying to calculate power in matlab

I have this function in matlab- 我在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 如果我在d的计算中删除了0.5幂,这将很好地工作。为什么我得到这个错误,在这条语句中似乎没有什么不对。我还检查了前几行中x1,x2,y1,y2的值,并且他们是

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

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 , ...). x1x2y1y2变量似乎是整数数据类型(例如uint8int32 ,...)。 They need to be double (or single ) to perform that operation. 它们必须是double (或single )才能执行该操作。 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: 还要注意,由于x1x2y1y2是标量,因此可以删除点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM