필터 지우기
필터 지우기

Finding minimum value between certain rows

조회 수: 1 (최근 30일)
Sha S
Sha S 2015년 7월 31일
편집: Andrei Bobrov 2015년 7월 31일
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!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 31일
편집: Azzi Abdelmalek 2015년 7월 31일
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

추가 답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by