how to arrange an array elemets to unique pairs?

4 次查看(过去 30 天)
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?

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2015-12-24
编辑:Azzi Abdelmalek 2015-12-24
X={'a1', 'b2', 'c3', 'd4'};
ii=1:numel(X)
jj=nchoosek(ii,2)
A=X(jj(:,1))'
B=X(jj(:,2))'

更多回答(1 个)

Image Analyst
Image Analyst 2015-12-24
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 个评论
vx2008
vx2008 2015-12-25
Thank you for your reply.
We can use 'nchoosek' to do this work better.
Thank you at all.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by