how to arrange an array elemets to unique pairs?

1 view (last 30 days)
for example, X={'a1', 'b2', 'c3', 'd4'};
I want to rearrange X as below:
A={'a1'; 'a1'; 'a1'; 'b2';'b2'; 'c3' } B={'b2'; 'c3'; 'd4' ;'c3';'d4';'d4'}
that means A, B:
'a1' 'b2'
'a1' 'c3'
'a1' 'd4'
'b2' 'c3'
'b2' 'd4'
'c3' 'd4'
Then how to get A and B according to X?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 24 Dec 2015
Edited: Azzi Abdelmalek on 24 Dec 2015
X={'a1', 'b2', 'c3', 'd4'};
ii=1:numel(X)
jj=nchoosek(ii,2)
A=X(jj(:,1))'
B=X(jj(:,2))'

More Answers (1)

Image Analyst
Image Analyst on 24 Dec 2015
It sounds like you want to generate A and B from X, rather than take an existing A and B and stitch them together horizontally like you'd do with C=[A,B].
But I can't figure out any general rule for generating A and B except to just manually do it from individual elements:
% This is what we're starting with:
X={'a1', 'b2', 'c3', 'd4'}
% This is what we want to generate
% A={'a1'; 'a1'; 'a1'; 'b2';'b2'; 'c3' }
% B={'b2'; 'c3'; 'd4' ;'c3';'d4';'d4'}
% Now, create A
A = [X(1); X(1); X(1); X(2); X(2); X(3)]
% Now, create B
B = [X(2); X(3); X(4); X(3); X(4); X(4)]
And you get A and B from X just like you wanted:
A =
'a1'
'a1'
'a1'
'b2'
'b2'
'c3'
B =
'b2'
'c3'
'd4'
'c3'
'd4'
'd4'
If you want it generalized to some different number of strings (rows), then you'll need to fill us in on what the rule for creating the order is, because I can't figure out any general rule. And, of course the rule for generating A is different than the rule for generating B so we'd need both rules.
  1 Comment
vx2008
vx2008 on 25 Dec 2015
Thank you for your reply.
We can use 'nchoosek' to do this work better.
Thank you at all.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!