Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何更正向量索引?_Matlab_Indexing_Statistics_Cluster Analysis - Fatal编程技术网

Matlab 如何更正向量索引?

Matlab 如何更正向量索引?,matlab,indexing,statistics,cluster-analysis,Matlab,Indexing,Statistics,Cluster Analysis,好的,我将数据聚类成簇,然后使用列对簇进行索引。数据是以运动矢量的形式存在的,因此我的数据经过聚类后将如下所示: [index x y x' y'] 例如: [1 3 5 4 6; 1 4 6 5 7; 2 3 5 4 6; 2 8 9 9 3; 3 2 3 2 4] 在上面的数组中有3个簇,簇1和簇2每个包含2个向量 我的问题是,有时我必须根据某些标准删除集群,可能会留下: [2 3 5 4 6; 2 8 9 9 3; 3 2 3 2 4] 我希望能够在删除后更正索引,使其从

好的,我将数据聚类成簇,然后使用列对簇进行索引。数据是以运动矢量的形式存在的,因此我的数据经过聚类后将如下所示:

[index x y x' y']
例如:

[1 3 5 4 6;
 1 4 6 5 7;
 2 3 5 4 6;
 2 8 9 9 3;
 3 2 3 2 4]
在上面的数组中有3个簇,簇1和簇2每个包含2个向量

我的问题是,有时我必须根据某些标准删除集群,可能会留下:

[2 3 5 4 6;
 2 8 9 9 3;
 3 2 3 2 4]
我希望能够在删除后更正索引,使其从1开始,以集群数结束。因此,在这种情况下,将2s替换为1s,将3s替换为2s

我确信一定有一种简单的方法可以使用for循环,但我已经尝试了一段时间,但仍然无法正确使用ti?

一个简单的调用将帮助您做到这一点。您可以使用它的第三个输出,使用新数据矩阵(索引向量)的第一列替换其第一列来分配每个唯一的新ID。此外,请确保使用
“稳定”
标志,以便它按照从上到下的顺序分配ID:

 %// Data setup
 A = [1 3 5 4 6;
      1 4 6 5 7;
      2 3 5 4 6;
      2 8 9 9 3;
      3 2 3 2 4];

 %-----
 B = A(3:end,:); %// Remove first two rows

 %// Go through the other IDs and reassign to unique IDs from 1 up to whatever
 %// is left
 [~,~,id] = unique(B(:,1), 'stable');

 %// Replace the first column of the new matrix with the new IDs
 B(:,1) = id; %// Replace first column with new IDs
我们得到:

>> B

B =

     1     3     5     4     6
     1     8     9     9     3
     2     2     3     2     4

假设您的矩阵名为
data
,请尝试以下操作:

>> data = [2 3 5 4 6;
           2 8 9 9 3;
           3 2 3 2 4]

data =

     2     3     5     4     6
     2     8     9     9     3
     3     2     3     2     4

>> data(:,1) = cumsum(diff(data([1 1:end], 1)) ~= 0) + 1

data =

     1     3     5     4     6
     1     8     9     9     3
     2     2     3     2     4

我完全忘记了独特性。:)+为了简单的快乐。如果我们中的任何一个都有所帮助,请考虑接受我们的答案之一。祝你好运如果我不知道
unique
:)+1,我会这么做。