我有两个这样的数据集
dataset1 (31x1倍):
0
32
45
8
...
91dataset2 (40x1双倍):
5
12
27
10
...
15我还有一个dataset1_index (31x1倍),用于索引来自更大数据集的datset1值。
2
5
6
9
...
58类似地,我有dataset2_index (40x1倍),用于索引来自同一个较大数据集的datset2值。
3
7
8
13
...
62我想将dataset1和dataset2组合成dataset3 (71x1双),但是dataset3中的值顺序应该遵循dataset1_index和dataset2_index的顺序(从小到大)。有人能帮忙吗?
发布于 2022-08-17 14:53:01
您可以创建一个包含索引和值的71x2矩阵,然后在索引列上创建sortrows(),并接受排序的值列。
B = sortrows(A,column)根据向量column中指定的列对A进行排序。例如,sortrows(A,4)根据第四列中的元素按升序对A行进行排序。
dataset1 = [0
32
45
8];
dataset2 = [5
12
27
10];
dataset1_index = [2
5
6
9];
dataset2_index = [3
7
8
13];
tmp = sortrows([dataset1_index dataset1; dataset2_index dataset2], 1);
% 2 0
% 3 5
% 5 32
% 6 45
% 7 12
% 8 27
% 9 8
% 13 10
dataset3 = tmp(:, 2);
dataset3_index = tmp(:, 1);https://stackoverflow.com/questions/73390526
复制相似问题