这是我尝试代码:如何在for循环中保存变量中的值而不用在MATLAB中覆盖?
for i = 1:length(X) %X is a CSV matrix 50x4
Y = X(i, :) % fetching each row of X
dist = pdist2(Y, X2, 'euclidean') %X2 is another matrix 100x4
sumOfdist = sum(dist);
end;
meanResult = mean(sum)
sumOfdist总是会在每次迭代覆盖,只有最后一次迭代因此我meanResult店的平均值。通过每次迭代存储所有值的总和并在forloop之外使用它来计算均值的最佳方式是什么?而不使用全局变量?
首先,您应该将您的变量“sum”重命名为其他值;你可以写sum1 = sum1 + sum(dist) – Anonymous
对,这不是总和 - 只是在这里调用它。 – 221b