counting the values and variables

I have a dataset 50x62
for example
S=['F' 'D' 'C' 'D' 'C';'C' 'C' 'F' 'D' 'F']
these variables are my output from one operation,now i want to count the number of variables
here i have 2 rows and number of variable is 3
so i need as
F D C
1 2 2
2 1 2
PLEASE HELP

1 comentario

kash
kash el 25 de Mzo. de 2012
wayne length gives value as 3,but i need as above

Iniciar sesión para comentar.

 Respuesta aceptada

Daniel Shub
Daniel Shub el 25 de Mzo. de 2012
What about a nice loop:
S=['F' 'D' 'C' 'D' 'C';'C' 'C' 'F' 'D' 'F'];
x = sort(unique(S));
fprintf('%c\t', x')
fprintf('\n')
for ii = 1:size(S, 1)
for jj = 1:length(x)
temp(jj) = length(find(S(ii, :) == x(jj)));
end
fprintf('%d\t', temp);
fprintf('\n')
end

11 comentarios

kash
kash el 25 de Mzo. de 2012
DAniel i get error Function is not defined for 'cell' inputs.
and plz tell in which variable these output are stored,i need it because for next operartion
Daniel Shub
Daniel Shub el 25 de Mzo. de 2012
If your S is a cell array and not a char array, then of course your are going to get an error. If S cannot be easily converted to a char array, then you are going to need a very different answer.
As for which variable the output is saved as, the output is not saved it is simply displayed. You didn't ask for saved output. Think about the loops and what temp is doing. How would you modify "temp" to save all the data... Try and figure it out yourself, and when you get stuck ask a new question.
kash
kash el 25 de Mzo. de 2012
Daniel S is an cel array ,can u please tell hoe to perform and save the output in variable,because i dont know hoew to process plz
Daniel Shub
Daniel Shub el 25 de Mzo. de 2012
No. If you are not willing to work at it, then I am not willing to help you.
kash
kash el 25 de Mzo. de 2012
Daniel i used char and then reshaped it but get some extra values
C D
0 62 1.168713e+001 8.909025e+000 1.202819e+001 9.658573e+000 9.563827e+000
Daniel Shub
Daniel Shub el 25 de Mzo. de 2012
You probably want to clear temp before the loop ...
kash
kash el 25 de Mzo. de 2012
ok daniel for displaying values in output
i did
[temp]=temp(jj),after temp(jj) = length(find(S(ii, :) == x(jj)));
i did not get the result,temp displays each value of row,so i tried to cancatanate it
kash
kash el 25 de Mzo. de 2012
for ii = 1:size(S, 1)
for jj = 1:length(x)
temp = length(find(S(ii, :) == x(jj)));temp(jj)=temp;
end
i tried this also but not getting exact answer
Daniel Shub
Daniel Shub el 25 de Mzo. de 2012
What does the ii loop do? What does the jj loop do? Are you saving the ii loop anywhere? What about temp(ii, jj)?
kash
kash el 25 de Mzo. de 2012
temp(ii) is 1,temp(jj)is 2
ii loop calculates the row of origal value(S),and jj calculates the column unique variables
kash
kash el 25 de Mzo. de 2012
i tried not getting for final time an u code for me for saving output in a variable

Iniciar sesión para comentar.

Más respuestas (1)

Wayne King
Wayne King el 25 de Mzo. de 2012
S = ['F' 'D' 'C' 'D' 'C';'C' 'C' 'F' 'D' 'F'];
NumVars = length(unique(S));

1 comentario

kash
kash el 25 de Mzo. de 2012
thanks wayne another
if i have
A=[1 2 3 2 1 ;2 3 1 1 1];
here i want to count the number of 1's,2s,3's for each row so i get
and store i different variables
A B C
2 2 1
3 1 1

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Mzo. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by