Find the index of the first values in ascending array that are greater than each values from random array without for loop
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jérémy
el 26 de Mzo. de 2018
Comentada: Jérémy
el 27 de Mzo. de 2018
Hello,
I have a random vector A=rand(n,1) and a given vector B(n,1) which is sorted in ascending values. I want to find the index of the first value of A that is greater than each values of my random vector. For example with:
A = [1 ; 6 ; 2 ; 8 ; 3]; B = [2 ; 4 ; 6 ; 8 ;10];
It would give: indexes = [1 4 2 5 2].
My current solution consist of using a for loop:
ix = zeros(N,1);
for i = 1:N
r = rand;
ix(i) = find(B>A,1);
end
I would like to be able to do the same without using a for loop in order to increase the speed of the operation.
Thanks
0 comentarios
Respuesta aceptada
Walter Roberson
el 26 de Mzo. de 2018
Second output of either histc or histcounts
3 comentarios
Guillaume
el 26 de Mzo. de 2018
Or just the first output of discretize
discretize(B, A)+1
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!