最大 - 最小距离计算

时间:2016-12-06 16:53:03

标签: matlab cluster-computing distance

我在数据上使用了Max-Min距离测量,但是我得到一个错误是Index超出矩阵尺寸。

我使用的代码是:

clc;
clear all;
close all; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numRows = 100;
A = rand(numRows, 2);      %// Example of input matrix
cent= [ 2.2  3.1; 4.5 5.1];
d= [];
sizeX = size(A,1);
numofC = size(cent,1);

for  i = 1 : sizeX
  for j = 1:numofC
    A= max(cent(j,:),A(i,:));
    B = min(cent(j,:),A(i,:));
  end 
    dist(i)= sum(B)/sum(A)   
 end 

任何帮助,请

1 个答案:

答案 0 :(得分:0)

您收到此错误,因为虽然您有输入矩阵A,但您尝试将max(cent(j,:),A(i,:))分配给同一变量A,从而覆盖内容。随后在循环中调用A将尝试调用第一次通过循环时覆盖的元素。

我从您的代码中做了一些更改;例如你也没有名为dist的矩阵。

clc;
clear;
close all; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numRows = 100;
A = rand(numRows, 2);      %// Example of input matrix
cent= [ 2.2  3.1; 4.5 5.1];
sizeX = size(A,1);
numofC = size(cent,1);
dist= zeros(numRows,1);


for  i = 1 : sizeX
    for j = 1:numofC
        X = max(cent(j,:),A(i,:));
        Y = min(cent(j,:),A(i,:));
        dist(i)= sum(X)/sum(Y)   
    end
end