How to create a histogram with weighted values from an array?

8 visualizaciones (últimos 30 días)
DL
DL el 22 de Jul. de 2020
Comentada: Vlatko Milic el 19 de Dic. de 2022
Hi all,
I am relatively new to MatLab so I need some assistance with this task. Basically, I have two arrays that I want to weigh the other with and create an overall histogram. To explain this better, I have an array that contains the bins (A) and an array with weighted values (B):
A = [70 90 80; 90 90 90; 80 60 70]
B = [0.5 0.8 1.0; 0.2 0.5 1.0; 0.3 0.2 0.5]
Ultimately, I want my bin counts to be:
60 bin: 0.2 70 bin: 1.0 80 bin: 1.3 90 bin: 2.5
Is there a way to use the A array to classify the weighted counts of array B and create a histogram?
Thanks in advance!

Respuesta aceptada

Alan Stevens
Alan Stevens el 22 de Jul. de 2020
This will do it (though there might well be a neater way!):
A = [70 90 80; 90 90 90; 80 60 70];
B = [0.5 0.8 1.0; 0.2 0.5 1.0; 0.3 0.2 0.5];
A = A(:); B = B(:); C = sortrows([A B]);
k = 0;
for i = 60:10:90
ix = C(:,1)==i;
k = k+1;
bin(k) = sum(C(ix,2));
ix = [];
end
  2 comentarios
DL
DL el 22 de Jul. de 2020
Thank you Alan! This works perfectly.
Vlatko Milic
Vlatko Milic el 19 de Dic. de 2022
Hi,
How do you get the histogram after the for-loop? With the bin properties you stated before? I tried the same procedure but could not get the correct histogram...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by