Hi! I am trying to find a way to calculate the 90% percentile of a matrix (1,n) . I provide an example below, and the value I would need to find is 0.292. (Note the matrix will changes in length). How could this be done?
[0.289, 0.254, 0.287, 0.292, 0.289, 0.267, 0.289, 0.304, 0.284, 0.282]

 Respuesta aceptada

Star Strider
Star Strider el 17 de Ag. de 2020

3 votos

Here are two possibilities, the second of which gives you the exact value in your Question:
v = [0.289, 0.254, 0.287, 0.292, 0.289, 0.267, 0.289, 0.304, 0.284, 0.282];
Out1 = prctile(v, 90)
Out2 = interp1(linspace(1/numel(v),1,numel(v)), sort(v), 0.9)
producing:
Out1 =
0.2980
Out2 =
0.2920
.

6 comentarios

Jonathan Moorman
Jonathan Moorman el 17 de Ag. de 2020
Out2 was exactly what I was looking for. Thanks!
Star Strider
Star Strider el 17 de Ag. de 2020
As always, my pleasure!
Jonathan Moorman
Jonathan Moorman el 1 de Sept. de 2020
Hi Star,
I've just now run into a problem with the line of code you sent. Until now it was working perfectly, but its currently giving a 90th percentile value that is not in the matrix. I gave a screenshot of the problem. Is there something incorrect with my code or is this something you have seen before? Thanks
Star Strider
Star Strider el 1 de Sept. de 2020
I do not have the vector to work with. (I can barely read the images of it.)
The 90th percentile may not actually be an element of the vector, simply what that element would be if it were there, since that by default uses linear interpolation.
See if this version does what you want, with the interpolation method now 'nearest':
Out2 = interp1(linspace(1/numel(v),1,numel(v)), sort(v), 0.9, 'nearest')
An anonymous function version of it would be:
prctlv = @(v) interp1(linspace(1/numel(v),1,numel(v)), sort(v), 0.9, 'nearest');
That will make it easier to work with. Call it as:
a90Per = prctlv(a90Export);
I tested that with the original ‘v’ and it again gave the desired result.
Jonathan Moorman
Jonathan Moorman el 3 de Sept. de 2020
Working great again. Thanks!
Star Strider
Star Strider el 3 de Sept. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

jonas
jonas el 17 de Ag. de 2020

1 voto

There is probably a one-liner for this, but I guess you could use
A = [0.289, 0.254, 0.287, 0.292, 0.289, 0.267, 0.289, 0.304, 0.284, 0.282];
B = sort(A);
id = round(numel(A).*0.9)
B(id)

Etiquetas

Preguntada:

el 17 de Ag. de 2020

Comentada:

el 3 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by