Creating a frequency table

Hello,
suppose I have a vector in legnth of 100 that contains a number between 1-4 how can I make a frequncy vector using the data in the vector? I'd tryed the function tabulate but if there is no 4 or 3 for example it will not be in the table and I want it to be there just with the value 0.
Thank's!

3 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 31 de Mayo de 2013
This is not clear
googo
googo el 31 de Mayo de 2013
Editada: googo el 31 de Mayo de 2013
Ok, sorry.
For example : [4 3 2 2 4 3 3] {only numbers between 0-4}
I'm looking for a function that will return a frequency vector :
0
0
2
3
4
Image Analyst
Image Analyst el 31 de Mayo de 2013
Editada: Image Analyst el 31 de Mayo de 2013
That's simply the histogram - I think - and you can use histc() or hist(). It's "I think" because I'm not sure why you say there are four 4's when I only see 2. The histogram would say there is two 4's in [4 3 2 2 4 3 3]. Perhaps you can explain your thought process around how you came up with four 4's.

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 31 de Mayo de 2013

0 votos

a=arrayfun(@(z) sum(ismember(x,z)),0:4)'
out=[(0:4)' a 100*a/numel(x)]

Más respuestas (2)

Andrei Bobrov
Andrei Bobrov el 31 de Mayo de 2013

1 voto

x=[1 1 2 2 1 2 ];
a = histc(x(:),0:4);
out = [(0:4)', a, a/sum(a)*1e2];
Azzi Abdelmalek
Azzi Abdelmalek el 31 de Mayo de 2013

0 votos

x=[1 1 2 2 1 2 ]
y=[x 5]
out=tabulate(y)
out(end,:)=[]

6 comentarios

googo
googo el 31 de Mayo de 2013
>> x=[1 1 2 2 1 2 ];
y=[x 5];
out=tabulate(y);
out(end,:)=[];
>> out
out =
1.0000 3.0000 42.8571
2.0000 3.0000 42.8571
3.0000 0 0
4.0000 0 0
it was suppose to be
0 0 0%
1 3 50%
2 3 50%
3 0 0%
4 0 0%
didn't understand the purpose of the "5" in your code. Thank's!
Azzi Abdelmalek
Azzi Abdelmalek el 31 de Mayo de 2013
You said between 1 and 4
googo
googo el 31 de Mayo de 2013
Editada: googo el 31 de Mayo de 2013
ho... sorry.. my mistake but anyway why it's 42.85% an not 50%?
Azzi Abdelmalek
Azzi Abdelmalek el 31 de Mayo de 2013
Editada: Azzi Abdelmalek el 31 de Mayo de 2013
If there is no 4 in your list, tabulate will not show 4 0 0 like you said, To resolve the problem,I've added 5 and removed its result
Iman Ansari
Iman Ansari el 31 de Mayo de 2013
x=[1 1 2 2 1 2 ];
[0:4 ;histc(x,0:4)]'
Azzi Abdelmalek
Azzi Abdelmalek el 31 de Mayo de 2013
Iman , make it as an answer

Iniciar sesión para comentar.

Categorías

Más información sobre Discrete Data Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 31 de Mayo de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by