Effacer les filtres
Effacer les filtres

how to arrange an array elemets to unique pairs?

2 vues (au cours des 30 derniers jours)
vx2008
vx2008 le 24 Déc 2015
Commenté : vx2008 le 25 Déc 2015
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?

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 24 Déc 2015
Modifié(e) : Azzi Abdelmalek le 24 Déc 2015
X={'a1', 'b2', 'c3', 'd4'};
ii=1:numel(X)
jj=nchoosek(ii,2)
A=X(jj(:,1))'
B=X(jj(:,2))'

Plus de réponses (1)

Image Analyst
Image Analyst le 24 Déc 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 commentaire
vx2008
vx2008 le 25 Déc 2015
Thank you for your reply.
We can use 'nchoosek' to do this work better.
Thank you at all.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by