Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using percentage function with accumarray

I have two arrays:

OTPCORorder = [61,62,62,62,62,62,62,62,62,62,62,62,65,65,...]
AprefCOR = [1,3,1,1,1,1,1,1,1,1,2,3,3,2,...]

for each element in OTPCORorder there is a corresponding element in AprefCOR. I want to know the percent of the number 1 for each set of unique OTPCORorder as follows:

OTPCORorder1 = [61,62,65,...]
AprefCOR1 = [1,0.72,0,...]

I already have this:

[OTPCORorder1,~,idx] = unique(OTPCORorder,'stable');
ANS = OTPCORorder1 = [61,62,65,...];

and I used to work with "accumarray" but I used the "mean" or "sum" function such as this:

AprefCOR1 = accumarray(idx,AprefCOR,[],@mean).';

I was just wondering if there exists a way to use this but with "prctile" function or any other function that gives me the percent of a specific element for example "1" in this case.

Thank you very much.

like image 630
chinkare_16 Avatar asked Jun 04 '15 05:06

chinkare_16


1 Answers

This could be one approach:

%// make all those non-zero values to zero
AprefCORmask = AprefCOR == 1;

%// you have done this
[OTPCORorder1,~,idx] = unique(OTPCORorder,'stable');

%// Find number of each unique values
counts = accumarray(idx,1);

%// Find number of ones for each unique value
sumVal = accumarray(idx,AprefCORmask);

%// find percentage of ones to get the results
perc = sumVal./counts

Results:

Inputs:

OTPCORorder = [61,62,62,62,62,62,62,62,62,62,62,62,65,65];
AprefCOR = [1,3,1,1,1,1,1,1,1,1,2,3,3,2];

Output:

perc =

1.0000
0.7273
     0
like image 156
Santhan Salai Avatar answered Sep 24 '22 17:09

Santhan Salai