Borrar filtros
Borrar filtros

I have an array of n elements like [1 2 4 8 16]. I want calculate frequency of all combinations. 1, 2 ,4 ,8, 16, 1+2, 1+4, 1+8, 1+16, 2+4, 2+8, 2+16, 4+8, 4+16, 8+16, 1+2+4, 1+2+8, 1+2+16, 1+2+4+8, 1+2+4+16, 1+2+4+8+16 How can i store output in array

2 visualizaciones (últimos 30 días)
1, 2 ,4 ,8 ,16
1+2, 1+4, 1+8, 1+16,
2+4, 2+8, 2+16,
4+8, 4+16,
8+16,
1+2+4, 1+2+8, 1+2+16,
1+2+4+8, 1+2+4+16,
1+2+4+8+16

Respuesta aceptada

Matt J
Matt J el 25 de En. de 2019
Editada: Matt J el 25 de En. de 2019
n=numel(yourVector);
mask=dec2bin(0:2^n-1,n)-'0';
mask(1,:)=[];
combs= mask*yourVector(:) ;
result = histcounts( combs , 1:max(combs)+1);
  4 comentarios
Stephen23
Stephen23 el 26 de En. de 2019
Editada: Stephen23 el 26 de En. de 2019
@tushar bhonsle: if you are using a MATLAB version prior to R2014b, then you will not have histcounts and will need to use histc instead, e.g.:
V = [1,2,4,8,16]
N = numel(V);
M = dec2bin(1:2^N-1)-'0';
C = M*V(:)
Z = histc(C, 1:max(C)+1)
@Matt J: surely it is easier to start from 1 than to delete the first row?
tushar bhonsle
tushar bhonsle el 26 de En. de 2019
Thanks, It's working but still how can i store in array and write output in excel file.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by