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
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.