how to arrange an array elemets to unique pairs?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
vx2008
el 24 de Dic. de 2015
Comentada: vx2008
el 25 de Dic. de 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?
0 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 24 de Dic. de 2015
Editada: Azzi Abdelmalek
el 24 de Dic. de 2015
X={'a1', 'b2', 'c3', 'd4'};
ii=1:numel(X)
jj=nchoosek(ii,2)
A=X(jj(:,1))'
B=X(jj(:,2))'
0 comentarios
Más respuestas (1)
Image Analyst
el 24 de Dic. de 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.
Ver también
Categorías
Más información sobre Elementary Math en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!