[英]How to find repeated cell vectors in matlab?
I have n
different length cell vectors, call it c{i}
, i=1,2,...,n. 我有
n
不同长度的像元向量,将其称为c{i}
,i = 1,2,...,n。
I wanna find those c{i} which equal with others, for example: 我想找到与他人相等的c {i},例如:
c{1}=[1 2 3 4 5 6]; c{2}=[1 3 5 7]; c{3}=[2 4 6 8];
c{4}=[1 4 6]; c{5}=[3 7]; c{6}=[2 4 6 8]; c{7}=[1 3 5 7];
I hope I can find [2 4 6 8]
and [1 3 5 7]
with a simple way instead of using two loops. 我希望我可以用一种简单的方法而不是使用两个循环来找到
[2 4 6 8]
和[1 3 5 7]
。
Thanks! 谢谢!
You can do it with unique
. 您可以通过
unique
方式做到这一点。 You need to convert vectors to strings, because unique
works with cell arrays of strings but not with cell arrays of numeric vectors. 您需要将向量转换为字符串,因为
unique
只能用于字符串的单元格数组,而不能用于数值向量的单元格数组。 After unique
, you can count how many strings (vector) are repeated with histc
, and them some indexing lets you retrieve the corresponding vectors: 在
unique
之后,您可以计算histc
重复多少个字符串(向量),并通过一些索引使您检索相应的向量:
strcell = cellfun(@(e) num2str(e), c, 'uniformoutput', 0); %// convert to strings
[~, ii, jj] = unique(strcell); %// apply unique. Second and third outputs needed
ind = find(histc(jj,min(jj)-.5:max(jj)+.2)>1); %// which appear more than once
result = c(ii(ind)); %// indexing to obtain corresponding original vectors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.