Using percentage function with drive

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 percentage of the number 1 for each unique OTPCORorder set 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 worked with the β€œdrive”, but I used the mean or sum function, such as:

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

I am just wondering if there is a way to use this, but with the function "prctile" or any other function that gives me the percentage of a particular element, like "1" in this case.

Thank you very much.

+6
source share
3 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 
+5
source

Here's a different approach without using accumarray . I think this is more readable:

 >> list = unique(PCORorder); >> counts_master = histc(PCORorder, list); >> counts = histc(PCORorder(AprefCOR == 1), list); >> perc = counts ./ counts_master perc = 1.0000 0.7273 0 

How the above code works, we first find those elements in PCORorder that are unique. As soon as we do this, we first calculate how many elements belong to each unique value in PCORorder through histc , using the bins to count at like this exact list. If you are using a newer version of MATLAB, use histcounts instead of the same syntax. Once we find the total number of elements for each value in PCORorder , we simply calculate how many elements correspond to PCORorder where AprefCOR == 1 , and then to calculate the percentage, you simply divide each entry in this list by the total number of elements from the previous list.

It will give you the same results as accumarray , but with less overhead.

+4
source

Your approach works, you only need to determine the appropriate anonymous function that will be used by accumarray . Let value = 1 be the value whose percentage you want to calculate. Then

 [~, ~, u] = unique(OTPCORorder); %// labels for unique values in OTPCORorder result = accumarray(u(:), AprefCOR(:), [], @(x) mean(x==value)).'; 

Alternatively, you can use sparse as follows. Create a two-row matrix such that each column corresponds to one of the possible values ​​in OTPCORorder . The first line counts how many times each value in OTPCORorder has the desired value in AprefCOR ; second row, how many times he did not.

 [~, ~, u] = unique(OTPCORorder); s = full(sparse((AprefCOR==value)+1, u, 1)); result = s(2,:)./sum(s,1); 
+3
source

Source: https://habr.com/ru/post/988493/


All Articles