how to arrange an array elemets to unique pairs?

조회 수: 1 (최근 30일)
vx2008
vx2008 2015년 12월 24일
댓글: vx2008 2015년 12월 25일
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에서 Elementary Math에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by