필터 지우기
필터 지우기

Max in bin using histc

조회 수: 1 (최근 30일)
K E
K E 2012년 6월 22일
I performed several tests to measure how y depends on x. I repeated the value of x in some of the tests. If x was repeated, the higher values of y are more reliable. Given a vector of x and y, I would like to find (a) the max value of y obtained for each value of x, i.e. selecting the max of the repeated tests plus any y value for which the value of x wasn't retested, and (b) all values of y from the repeated tests that were not the max value. I believe this can be done with a clever application of histc. Here is an example,
x = [ 219 292 292 365 365 365 402 438 438 511 ] ; % Independent variable with some repeats
y = [ 69.9 76.4 72.6 61.6 48.9 77.7 77.6 76 78.9 73.3 ] ; % Dependent variable
iMax = ?? ; % Index to max value of y obtained for each value of x; also include values of y for which x value wasn't retested
plot(x(iMax), y(iMax), 'b') ; hold on ;
iNotMax = ?? % For repeated tests, find index to all non-max values of y
plot(x(iNotMax), y(iNotMax), 'b*') ;
  댓글 수: 1
K E
K E 2012년 6월 22일
I think I can find (a) as follows, then overplot all values to see (b) visually, but perhaps there is a better approach?
%%
[ nInBin, indexToBin ] = histc(x, unique(x)) ;
iGoodBin = indexToBin > 0 ;
maxValue = accumarray(col_vect(indexToBin(iGoodBin)), ...
col_vect(y(iGoodBin)), [], @max, NaN) ;

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 6월 22일
second variant
[a b b] = unique(x);
out = [a',accumarray(b',y',[],@max)];
  댓글 수: 1
K E
K E 2012년 6월 26일
I will accept this one because it is shorter but both solutions work and were useful for learning so thanks to both authors.

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

추가 답변 (1개)

the cyclist
the cyclist 2012년 6월 22일
Here's one way:
x = [ 219 292 292 365 365 365 402 438 438 511 ];
y = [ 69.9 76.4 72.6 61.6 48.9 77.7 77.6 76 78.9 73.3 ];
z = sortrows([x; y]');
[~,indexToLastUniqueX] = unique(x,'last');
zUnique = z(indexToLastUniqueX,:)
  댓글 수: 1
K E
K E 2012년 6월 26일
Thanks, this solution works and I appreciate it. I was stumped on how to accept 2 answers, seems I can't, so I accepted the shorter one.

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by