好的,所以我将数据聚类到集群中,然后使用列对集群进行索引。数据是以运动矢量的形式存在的,所以我的数据在聚类后会是这样的:
[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开始,以聚类的数量结束。所以在这种情况下,用1代替2s,用2s代替3s。
我确信肯定有一种使用for循环的简单方法,但我已经尝试了一段时间,但就是搞不懂?
发布于 2015-09-16 23:11:06
对unique
的简单调用将帮助您做到这一点。您可以使用它的第三个输出来分配每个唯一的新ID,使用新数据矩阵(索引向量)的第一列来替换它的第一列。此外,请确保使用'stable'
标志,以便它按从上到下的顺序分配it:
%// 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
发布于 2015-09-16 23:14:30
假设您的矩阵名为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
https://stackoverflow.com/questions/32612283
复制相似问题