Finding minimum value between certain rows
Afficher commentaires plus anciens
Hi,
I have a 9x3 matrix.
A= [0.08 34.1 2; 0.03 34.2 2; 0.04 34.3 2; 0.05 34.4 1; 0.07 34.5 1; 0.04 34.6 1; 0.02 34.7 1; 0.03 34.8 2; 0.08 34.9 2]
I want to find the minimum value in the first column but not for all the rows together. As you can see the first three rows have a value of 2 in the third column so I want to find the minimum value in the first column for the first three rows. Then rows 4 to 7 all have a 1 in the last column so I want to find the minimum value in column 1 from rows 4-7. Then rows 8 to 9 have a 2 in the third column so I want to find the minimum value in the first column in rows 8-9. In the end I want this...
B= [0.03 34.2 2; 0.02 34.7 1; 0.03 34.8 2]
The second thing I want to do is similar to the first thing but instead of finding the minimum value I want to find the average of the column 1 from Matrix A between certain rows (same row selection as when finding the minimum). In the end I want this...
C= [0.05 2; 0.045 1; 0.055 2]
I hope that made sense. Thanks!
Réponse acceptée
Plus de réponses (1)
Andrei Bobrov
le 31 Juil 2015
Modifié(e) : Andrei Bobrov
le 31 Juil 2015
i0 = [true;diff(A(:,3))~=0];
ii = cumsum(i0);
am = accumarray(ii,(1:numel(ii))',[],@(x){A(x(A(x,1)==min(A(x,1))),:)});
B = cat(1,am{:});
C = [accumarray(ii,A(:,1),[],@mean),A(i0,3)];
2 commentaires
Azzi Abdelmalek
le 31 Juil 2015
Modifié(e) : Azzi Abdelmalek
le 31 Juil 2015
I will correct it like this
am= accumarray(ii,(1:numel(ii))',[],@(x) {A(find(A(x,1)==min(A(x,1)))+min(x)-1,:)})
Or
am= accumarray(ii,(1:numel(ii))',[],@(x) {A([ logical(zeros(min(x)-1,1));A(x,1)==min(A(x,1))],:)})
Andrei Bobrov
le 31 Juil 2015
Thank you Azzi. I am corrected.
am = accumarray(ii,(1:numel(ii))',[],@(x){A(x(A(x,1)==min(A(x,1))),:)});
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!