t = [ 4 2
2 6
6 9];
c = [ 2
3
1];
现在,我想根据t
索引安排c
;我需要根据t
获得c
新排序:
t = [ 2 6
4 2
6 9];
如何根据我拥有的一个索引对矩阵进行排序?
以下是我的尝试:
h = cell(1,1);
h{1,1} = t;
h{1,1}(c)
答案 0 :(得分:0)
此代码使用matlab中可用的键:值映射。您可以将其合并到一个函数中,并针对我已重新标记t
的任意矩阵c
和keySet
进行调整
t = [ 4 2;2 6; 6 9]; % original matrix
keySet = [ 2 3 1]; % original c indexing vector
valueSet= cell(1,size(t,1)); % create a cell matrix to hold the row elements of t
for i =1:size(t,1)
valueSet{1,i} = t(i,:); % insert row elements into cell array
end
mapObj = containers.Map(keySet,valueSet); % map the keys to the values
keySet = sort(keySet,'descend'); % reorder the keys
for j=1:size(keySet,2)
t(j,:) = mapObj(keySet(1,j)); % rebuild matrix t with new order
end