フィルターのクリア

how to automate the count of consecutive values in a cell array

1 回表示 (過去 30 日間)
pamela sulis
pamela sulis 2016 年 3 月 30 日
回答済み: Azzi Abdelmalek 2016 年 3 月 30 日
Hi! I have a cell array with this values
a={'2' '3' '4' '5'; '2' '2' '3' '1'; '2' '2' '2' '1';'1' '2' '3' '3'; '1' '2' '3' '4'};
a =
'2' '3' '4' '5'
'2' '2' '3' '1'
'2' '2' '2' '1'
'1' '2' '3' '3'
'1' '2' '3' '4'
and I want to count the number of consecutive values in the colums I consider the first two columns and I find the couples:
('2' '3'), ('2' '2') ('1' '2')
I want to find the number of times that this couples are presents in the first two colums
('2' '3') --> 1
('2' '2') --> 2
('1' '2') --> 2
After I consider the triple
('2' '3' '4'), ('2' '2' '3'), ('2' '2' '2'), ('1' '2' '3')
I want to find the number of times that this couples are presents in the first three columns:
('2' '3' '4')--> 1
('2' '2' '3')--> 1
('2' '2' '2')--> 1
('1' '2' '3')--> 2
After I consider the quadruple
('2' '3' '4' '5'), ('2' '2' '3' '1'), ('2' '2' '2' '1'), ('1' '2' '3' '3') ('1' '2' '3' '4')
I want to find the number of times that this couples are presents in the four columns:
('2' '3' '4' '5')--> 1
('2' '2' '3' '1')--> 1
('2' '2' '2' '1')--> 1
('1' '2' '3' '3')--> 1
('1' '2' '3' '4')--> 1
Can you help me?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 30 日
x={'2' '3' '4' '5'; '2' '2' '3' '1'; '2' '2' '2' '1';'1' '2' '3' '3'; '1' '2' '3' '4'};
[n,m]=size(x)
for k=2:m
a=x(:,1:k)
b=arrayfun(@(x) strjoin(a(x,1:k),'/'),(1:size(a,1))','un',0);
[ii,jj,kk]=unique(b,'stable');
out{k-1,1}=[a(jj,:) num2cell(accumarray(kk,1))];
end
out{1}
out{2}
out{3}

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by