你在做什么叫做“并行排序数组”或“排序并行数组”。您可以使用这些术语搜索如何操作的指南。但是,下面是一些代码,显示了在MATLAB中执行此操作的一种方法:
unsorted_keys = [95, 37, 56, 70, 6, 61, 58, 13, 57, 7, 68, 52, 98, 25, 12];
unsorted_strings = cell(size(unsorted_keys));
unsorted_strings = {'crumply', 'going', 'coyotes', 'aficionado', 'bob', 'timeless', 'last', 'bloke', 'brilliant', 'reptile', 'reptile', 'reptile', 'reptile', 'reptile', 'reptile'};
[sorted_keys, indicies] = sort(unsorted_keys);
% indicies = [5, 10, 15, 8, 14, 2, 12, 3, 9, 7 6, 11, 4, 1, 13]
% So, the 5th element of unsorted_keys became the 1st element of sorted_keys
% the 10th element of unsorted_keys became the 2nd element of sorted_keys
% the 15th element of unsorted_keys became the 3rd element of sorted_keys
% the 8th element of unsorted_keys became the 4th element of sorted_keys
% and so on.....
% sorted_keys == unsorted_keys(indicies)
sorted_strings = unsorted_strings(indicies);
您的标记是Python和Matlab。你使用哪种语言? –
Python字典可能在此处有用,因为您无法真正将整数声明为变量并将其分配给它。 – BusyAnt
使用Matlab,您可以创建一个单元格矢量并将每个字符串分配给它的相应位置。 A {103} ='QWE';一个{13} ='13'等等......虽然如果大多数职位都是空的,它可能是低效率的。 –