【问题标题】:MATLAB calculating distances in a loopMATLAB循环计算距离
【发布时间】:2014-11-18 03:12:31
【问题描述】:

我正在加载一个 .csv 文件以在 matlab 中进行一些计算。该文件本身有大约 1600 行,但我只对一个子集感兴趣。

load file.csv; %load file

for i = 400:1200  %rows I am interested in

  rh_x= file(i,60); % columns interested, in column 60 for the x, 61 for y
  rh_y= file(i,61);

  rh_x2 = file(i+1, 60); % next point (x,y)
  rh_y2 = file(i+1, 61);

  p1 = [rh_x, rh_y];
  p2 = [rh_x2, rh_y2];

  coord = [p1, p2]; 
  Distan = pdist(coord, 'euclidean');  ****
  disp(Distan);
end

我的 Distan 变量(距离公式)中没有存储任何内容,我尝试在其中输入两个点。为什么会这样?我只想计算第 400-1200 帧的第 60 行和第 61 行中所有点对的距离公式。

谢谢。

【问题讨论】:

  • 你能打印coord并确认它是正确的矩阵吗?
  • 是的,我在上面更改了它并显示它是正确的。

标签: matlab for-loop euclidean-distance


【解决方案1】:

将您的 coord 分配更改为以下内容:

coord = [p1; p2];

按照您的方式,它将所有 x、y 对存储在同一行上,作为 1x4 矩阵。上述方法将其存储为 2x2 矩阵,pdist 给出了答案。

【讨论】:

  • 非常感谢,就是这样!我已经有一段时间没有接触 matlab 了。
猜你喜欢
  • 2018-02-13
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 2016-02-04
相关资源
最近更新 更多