Pair comparison in from other rows

I have:
A1=[3 4]
A2=[3 1 4]
A3=[3 2 1 4]
want to convert as:
(3,4)
(3,1)(1,4)
(3,2)(2,1)(1,4)
Now I want to check, is any pair common in all rows (except first rows)
For example: combination (1,4) is common in 2 and 3 row. In the result, I want this pair. Please help me to complete this program.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 5 月 26 日

1 投票

A = {A1,A2,A3};
C = cellfun(@(x) hankel(x(1:end-1),x(end-1:end)),A,'un',0);
[a,~,c] = unique(cat(1,C{:}),'rows');
out = a(histc(c,1:size(a,1)) > 1,:);

5 件のコメント

raj singh
raj singh 2016 年 5 月 26 日
編集済み: Image Analyst 2016 年 5 月 26 日
Its really work great. Thank you very much. But when i increase the A means A4 then it not work correctly. For Example:
A1=[1 4];
A2=[1 3 4];
A3=[1 2 3 4];
A4=[1 2 6 3 4];
I want common pair from all row except first row, it means my answer should be (3 4) not (1 2)(3 4). Please reply .......
Andrei Bobrov
Andrei Bobrov 2016 年 5 月 26 日
編集済み: Andrei Bobrov 2016 年 5 月 26 日
A1 = [1 4]; A2 = [1 3 4]; A3 = [1 2 3 4]; A4 = [1 2 3 4 6];
A = {A1;A2;A3;A4};
C = cellfun(@(x) hankel(x(1:end-1),x(end-1:end)),A,'un',0);
[m,~] = cellfun(@size,C);
[a,~,c] = unique(cat(1,C{:}),'stable','rows');
q = accumarray(c, repelem(m,m),[],@(x){x});
out = a(cellfun(@(x)all(ismember((2:numel(A))',x)),q),:);
Image Analyst
Image Analyst 2016 年 5 月 26 日
編集済み: Image Analyst 2016 年 5 月 26 日
What are the rules for what the A's can be? for example if
A1=[1 4];
A2=[1 3 4];
A3=[1 2 6 3 4];
A4=[1 2 6 3 4 7 8];
Andrei's code gives an empty output but there are 3 repeated pairs (ignoring the first column): (2,6), (6,3), and (3,4). What is the use case anyway? Homework? Otherwise it seems like a really quirky thing to want to do.
raj singh
raj singh 2016 年 5 月 28 日
Thanks Andrei sir, but i am using 2012a MATLAB, in which repelem and accumarray is not working. I don't have Matlab2016a. There is any other option to run run your given program.
raj singh
raj singh 2016 年 5 月 28 日
Hello Image, Actually [A] represent the paths from one node to other node and i want common line in that paths. Do you have any suggestion for this ppg.

サインインしてコメントする。

その他の回答 (1 件)

raj singh
raj singh 2016 年 5 月 29 日

0 投票

A1=[1 4];
A2=[1 3 4];
A3=[1 2 3 4];
A4=[1 2 6 3 4];
I want common pairs from all row (vectors) except first row. It means my answer will be (3 4) and (1 2). First answer of Andrei sir, works perfect. May this is possible to print like this:
(Total no of row).........(Pair)............(How many times)
4 (3 4) 3
4 (1 2) 2

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2016 年 5 月 26 日

回答済み:

2016 年 5 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by