MATLAB: Finding minimum value between certain rows

minimumrow

Hi,
I have a 9×3 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!

Best Answer

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