How to assign value 2 for >=90 percentile and 1 for < 90 percentile in a single column matrix?

2 次查看(过去 30 天)
I have a column matrix of 414 elements. I want to replace these values in binary format with values greater than or equal to 90 percentile as 2 and less than 90 percentile as value 1 in second column in order to keep the original data in first column. I will appreciate for your kind suggestions.

回答(2 个)

Garv Agarwal
Garv Agarwal 2023-7-26
Hi Devendra,
From my understanding, you want to do a binary classification of your data, classifying elements greater than or equal to 90 percentile as 2 and others as 1.
You can find out the 90th percentile value by using the prctile function-
value90 = prctile(vector, 90)
Then find out the indices of elementes greater than 90th percentile -
idx = find(vector>=value90)
Create a ones matrix and replace the values greater than 90th percentile -
newVector = ones(1,414);
newVector(idx) = 2;
You can then concatenate these column vectors to get a two column matrix-
matrix=[vector,newVector]
For more information you can refer to the following documentations -

Pranavkumar Mallela
编辑:Pranavkumar Mallela 2023-7-27
Hi,
I understand that you want to replace values in a column matrix depending on whether the element is greater than or lesser than the 90th percentile.
You can use the 'prctile' function to do this. Please find the code for the same below.
x = (1:10)'; % your column data
p = prctile(x, 90);
y = x>=p;
y = y+1;
x = [x y]; % x now contains another column with the binary values
To know more about the 'prctile' function, please refer to the following documentation: https://mathworks.com/help/matlab/ref/prctile.html
Hope this helps! Thanks!

Community Treasure Hunt

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

Start Hunting!

Translated by