How do I Iterate over similar elements in an array

Hi, I have an input text file that looks like what's below (the actual text file is 1000 lines so I'm just providing a sample). Anyway, I need to compute the mean, and variance (to fit data into a Gaussian). The last column of input is "class" I need to compute the values for each unique class. So for each of the 10 classes I need the mean and variance. But the data is not ordered. The output should look like: Class %d Column %d Mean %.2f Variance %.2f. How can I accomplish this goal? Thanks a ton
0.5000 0.4600 0.6400 0.3600 0.5000 0 0.4900 0.2200 1
0.5300 0.5600 0.4900 0.4600 0.5000 0 0.5200 0.2200 1
0.5200 0.5300 0.5800 0.6900 0.5000 0 0.5000 0.2200 1
0.6700 0.6200 0.5400 0.4300 0.5000 0 0.5300 0.2200 1
0.4500 0.5700 0.3000 0.1700 0.5000 0 0.5100 0.2200 3
0.6800 0.5300 0.4400 0.3100 0.5000 0 0.5100 0.2200 1
0.3800 0.4200 0.3100 0.3500 0.5000 0 0.5200 0.2200 3
0.4800 0.4600 0.5800 0.2600 0.5000 0 0.4400 0.2200 2

4 comentarios

jonas
jonas el 28 de Feb. de 2018
Just to clarify, you want one set of values per column and class?
Here's the prompt of the question: The output of your code should contain one line for each dimension of each class. Such a line should look like this: Class %d, dimension %d, mean = %.2f, variance = %.2f
Okay, I would suggest using logical indexing to group classes. For example, if A is your matrix then the mean for class 1 would be:
mean(A(A(:,end)==1,:))
That worked perfectly! Thank you so much

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 28 de Feb. de 2018

0 votos

You can use unique to get the list of classes, and then you can loop over the submatrix with logical indexing (which you might need to convert to indices with find).

5 comentarios

I did what Jonas suggested mean(A(A(:,end)==1,:)) And it worked great. Thank you though
His suggestion is similar to what I meant. You can still get a list of all classes with unique:
list_of_classes=unique(A(:,end));
for class_index=1:length(list_of_classes);
mean_list(class_index)= mean(A(A(:,end)==list_of_classes(class_index),:));
end
Thanks for the clarification. Can you tell me what are the pros and cons of using unique in this case? I am still new to matlab. Also why can't we just say mean(A(:,end) == ..)? I am confused as to why we use mean(A(A...))
There aren't really cons. It just looks at the data you put in (in this case the last column of A), and returns a list of all unique values.
As for your second question, you should look at a small example:
A=[1 1;2 1;5 2];
list_of_classes=unique(A(:,end));
%is equivalent to:
%list_of_classes=unique([1;1;2]);
%A(:,end)==1 will return a logical vector:
pos=A(:,end)==1;%returns [true;true;false]
%you can use this vector for logical indexing:
data=A(pos,:);%returns [1;2]
%and then we can calculate the mean:
mean_list=mean(data);
Now you can see that using your suggested code would result in calculating the mean of the logical position vector, not the data.
Thank you for the great explanation. I 100% understand what's going on now.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 28 de Feb. de 2018

Comentada:

el 2 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by