New variable based on a 'percentile' condition

1 view (last 30 days)
I have a double variable A with 6000 rows and 3 columns:
  • C1 gives the year;
  • C2 gives a code (no repetitions within each year);
  • C3 gives a number;
C1 C2 C3
A=[1983 11 54
1983 13 24
1983 16 32
1983 20 11
1983 25 14
1983 28 23
1983 29 19]
B, is a cell variable with 1 row and 31 columns (each column corresponds to a year) that gives the 90th percentile of A(:,3) per year:
B={53 49.3 51.7 49.2 48 40 41 44.6 }
I am trying to obtain a new variable A2 that would be equal to A, but would only consider the cases in which A(:,3) > B(1,:).
For example, I have already done something similar but with different conditions, only in these cases my condition would not change from year to year:
numMean=A(A(:,3)>=42.8,[1 2 3]);
num25=A(A(:,3)>=25,[1 2 3]);
Thank you very much for your help.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 26 Aug 2014
Edited: Azzi Abdelmalek on 26 Aug 2014
B={53 49.3 51.7 49.2 48 40 41}
A=[1983 11 54
1983 13 24
1983 16 32
1983 20 11
1983 25 14
1983 28 23
1983 29 19]
out=A(all(bsxfun(@gt, A(:,3),cell2mat(B)),2),:)

More Answers (1)

Kelly Kearney
Kelly Kearney on 26 Aug 2014
Could do it all it one step with accumarray. Though this way doesn't preserve the initial order... not sure if that is necessary or not for your task.
% Fake data
n = 100;
A = [floor(rand(n,1)*5) + 1983 ones(n,1) round(rand(n,1)*100)];
% Find values >90% of their year
[unqyr, blah, ia] = unique(A(:,1));
nyr = length(unqyr);
atop = accumarray(ia, A(:,3), [nyr 1], @(x) {x(x > prctile(x,90))});
cell2mat(atop)

Products

Community Treasure Hunt

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

Start Hunting!