![](/img/trans.png)
[英]How to find out consecutive sequence of numbers in two different vectors in MATLAB?
[英]How to find repeated cell vectors in matlab?
我有n
不同長度的像元向量,將其稱為c{i}
,i = 1,2,...,n。
我想找到與他人相等的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];
我希望我可以用一種簡單的方法而不是使用兩個循環來找到[2 4 6 8]
和[1 3 5 7]
。
謝謝!
您可以通過unique
方式做到這一點。 您需要將向量轉換為字符串,因為unique
只能用於字符串的單元格數組,而不能用於數值向量的單元格數組。 在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.