Finding minimum value between certain rows

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

Azzi Abdelmalek
Azzi Abdelmalek le 31 Juil 2015
Modifié(e) : Azzi Abdelmalek le 31 Juil 2015
idx= [diff(A(:,3)')==0 0];
ii2=find(idx==0);
ii1=[1 ii2(1:end-1)+1];
B=zeros(numel(ii1),size(A,2));
C=zeros(numel(ii1),2);
for k=1:numel(ii1)
jj=ii1(k):ii2(k);
[minv,jdx]=min(A(jj,1));
B(k,:)=A(jj(jdx),:);
C(k,:)=[mean(A(jj,1)) A(jj(1),3)];
end
B,C

Plus de réponses (1)

Andrei Bobrov
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
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))],:)})
Thank you Azzi. I am corrected.
am = accumarray(ii,(1:numel(ii))',[],@(x){A(x(A(x,1)==min(A(x,1))),:)});

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by