I have random matrix A 120x6. In last column I have only values 1 or 2. I want to find the min, max, median, and mean of first column for this rows, where in 6 column is 1.
You can first find the index where columm 6 is one, save it in a vector and then use it as a mask for your first columm, in this way you also vectorize your code, which runs faster in Matlab :
A = rand(120,6)*2;
A(:,6) = ceil(A(:,6)); % Values here can be only either 1 or 2
IndexToTakeAverageAndEtc = find(A(:,6)==1); % Find all rows where A(:,6)==1
Mean = mean(A(IndexToTakeAverageAndEtc,1)); % Take the mean only for the rows where the last columm is 1
i must to have mean from values from column 1 where in the same row , but i the 6 column is number 1. in this example i must to have mean from (1 ,3 ,2,2,3).
Sorry, I made a typo in my initial code in getting the value from the frist columm, just change the 6 for a 1 (I also edited the answer), this give the result that you want:
A = [6 8 2 9 5 2
1 8 6 10 5 2
1 10 2 7 2 1
3 3 2 7 2 1
2 10 8 3 1 1
2 1 4 6 4 1
10 9 2 2 9 2
3 8 5 2 5 1
4 3 9 8 6 2
4 8 4 7 7 2
10 1 4 9 7 2];
A(:,6) = ceil(A(:,6)); % Values here can be only either 1 or 2
IndexToTakeAverageAndEtc = find(A(:,6)==1); % Find all rows where A(:,6)==1
Mean = mean(A(IndexToTakeAverageAndEtc,1)) % Take the mean only for the rows where the last columm is 1
Impossible de terminer l’action en raison de modifications de la page. Rechargez la page pour voir sa mise à jour.
Translated by
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.