Sorting of arrays based on another array
Mostrar comentarios más antiguos
I have two arrays:
data_array=[4.4, 7.2, 10.1, 1.1, 5.5, 8.3, 2.2, 6.2, 3.3, 9.1, 1.3]
test_array=[2, 5, 9, 4, 10, 8, 7, 3, 6, 1, 1]
I need the output_array = [2.2, 5.5, 9.1, 4.4, 10.1, 8.3, 7.2, 3.3, 6.2, 1.1, 1.3]
i.e., I need to arrange the data_array based on each element wise smallest difference of the two arrays in a unique manner (meaning: in the example test_array, value '1' is present twice whoes smallest difference->0.1 with values of data_array, hence 1.1 in output array, but for second value '1' in the test_array it should take the next smallest difference-> 0.3, hence 1.3 in output array)
This is an example. I want to execute this for arrays of larger lengths (in thousands/millions)
Thank you in advance.
1 comentario
This takes around 6.91 sec to run for 1e5 elements on here -
da=[4.4, 7.2, 10.1, 1.1, 5.5, 8.3, 2.2, 6.2, 3.3, 9.1, 1.3];
temp=da;
ta=[2, 5, 9, 4, 10, 8, 7, 3, 6, 1, 1];
n=numel(ta);
y=zeros(1,n);
for k=1:n
[~,y(k)]=min(abs(temp-ta(k)));
temp(y(k))=NaN;
end
da(y)
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!