how to generate and cover all the possible arrays using two enties

2 visualizaciones (últimos 30 días)
R@SH
R@SH el 30 de Sept. de 2021
Comentada: R@SH el 1 de Oct. de 2021
Hi Dear All,
I have an array of 1*4, in which each array can be of two strings 'H' or 'C'. to elaborate on it more this can be like below:
[H H H H] or [H H H C] or [H C C C] or ..........[C C C C}
in total there would be 2*2*2*2=16 case for that.
I would like to generate all the 16 cases and following that I want to proceed the calculations. I dont know how to form these 16 cases and cover all the possible cases for that.
I would appreciate your support in advance.

Respuesta aceptada

DGM
DGM el 30 de Sept. de 2021
If you mean actual strings instead of chars, then:
A = dec2bin(0:15);
A(A=='1') = 'H';
A(A=='0') = 'C';
A = string(num2cell(A))
A = 16×4 string array
"C" "C" "C" "C" "C" "C" "C" "H" "C" "C" "H" "C" "C" "C" "H" "H" "C" "H" "C" "C" "C" "H" "C" "H" "C" "H" "H" "C" "C" "H" "H" "H" "H" "C" "C" "C" "H" "C" "C" "H" "H" "C" "H" "C" "H" "C" "H" "H" "H" "H" "C" "C" "H" "H" "C" "H" "H" "H" "H" "C" "H" "H" "H" "H"
  4 comentarios
DGM
DGM el 1 de Oct. de 2021
You could do something like this
dec2bin(0,2)
ans = '00'
dec2bin(0,4)
ans = '0000'
That would allow you to explicitly set the field width
R@SH
R@SH el 1 de Oct. de 2021
I appreciate your full support dear DGM.

Iniciar sesión para comentar.

Más respuestas (1)

Akira Agata
Akira Agata el 1 de Oct. de 2021
Another possible solution:
[p,q,w,s] = ndgrid({'H','C'});
A = [p(:),q(:),w(:),s(:)];
disp(A)
{'H'} {'H'} {'H'} {'H'} {'C'} {'H'} {'H'} {'H'} {'H'} {'C'} {'H'} {'H'} {'C'} {'C'} {'H'} {'H'} {'H'} {'H'} {'C'} {'H'} {'C'} {'H'} {'C'} {'H'} {'H'} {'C'} {'C'} {'H'} {'C'} {'C'} {'C'} {'H'} {'H'} {'H'} {'H'} {'C'} {'C'} {'H'} {'H'} {'C'} {'H'} {'C'} {'H'} {'C'} {'C'} {'C'} {'H'} {'C'} {'H'} {'H'} {'C'} {'C'} {'C'} {'H'} {'C'} {'C'} {'H'} {'C'} {'C'} {'C'} {'C'} {'C'} {'C'} {'C'}

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by