简体   繁体   English

从向量获得的值的总和

[英]summation of values which are obtained from a vector

Let Q be a nx2 vector, which contains n (2-dimensional) coordinates. 令Q为nx2向量,其中包含n个(二维)坐标。 I want to calculate the distance between the i and i+1-th coordinate for all integers i in the vector and then add it up. 我想为向量中所有整数i计算第i个坐标和第i + 1个坐标之间的距离,然后将其相加。

I came up with the following: 我想出了以下几点:

syms d

symsum(sqrt(sum((Q(d,:)-Q(d+1,:).^2))),d,1,n-1);

However, I get the following error: 但是,出现以下错误:

??? Error using ==> sym.sym>notimplemented at 2653
Function 'subsindex' is not implemented for MuPAD symbolic objects.

Error in ==> sym.sym>sym.subsindex at 1359
            notimplemented('subsindex');

Error in ==> test4 at 4
symsum(sqrt(sum((Q(d,:)-Q(d+1,:).^2))),d,1,n-1);

Alternatively I could of course use a for loop, but I suspect that this is probably not the neatest and quickest solution. 另外,我当然可以使用for循环,但是我怀疑这可能不是最快捷的解决方案。

I think it is the way you are implementing the syms variables. 我认为这是实现syms变量的方式。 You are using the symbolic objects d to reference the "d-th" element in the vector Q (your error) and then use (steal) the embedded loop from symsum to go through the vector. 您正在使用符号对象d来引用向量Q中的“第d”个元素(您的错误),然后使用(窃取)从symsum嵌入的循环来遍历向量。

Also symbolic objects are time consuming. 此外,符号对象也很耗时。 Just do the for/while loop it will be easier and faster. 只需执行for / while循环,它将更轻松,更快。

gl. gl。

If I am reading this correctly you just want the Euclidean distance between pairs of points in your nx2 matrix. 如果我没看错的话,您只想要nx2矩阵中的点对之间的欧几里得距离。 So all you have to do is this: 所以您要做的就是:

>> Q = rand(10,2)  %Define Q

Q =

    0.6557    0.7060
    0.0357    0.0318
    0.8491    0.2769
    0.9340    0.0462
    0.6787    0.0971
    0.7577    0.8235
    0.7431    0.6948
    0.3922    0.3171
    0.6555    0.9502
    0.1712    0.0344

>> distPairs =  sum(diff(Q,1,1).^2,2); %Distance between adjacent coordinates
>> totalDist = sum(distPairs)  %Sum all of the pairwise distances.

totalDist =

    4.0486

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

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