How to permute Permute Vector according to a permutation for its associated labels

Hi, I need to permute a nx3 vector
251.6147 99.4768 200.1291
253.8050 94.4682 95.3241
245.3254 114.3247 255.0000
...
according to their associated label, e.g.
1
2
3
2
3
4
5
with the new permutation being a vector like [5 4 3 2 1] such that 1-->5, 2-->4, 3-->3, 4-->2, 5-->1. There are multiple rows with different values associated to the same label. I would like to associate the first row from old label 1 with the first row from old label 5, then the second row from old label one to second row from old label 5. If there are fewer rows that belong to one label or the other, the next row should be chosen modulo the number of rows that belong to the label --- until the new n*3 vector is "permutated", and filled correctly such that the association of rows to new label ordering holds.

Risposte (1)

My solution is the following:
function [indicesOut] = permuteIndices(indicesIn,permutation)
%PERMUTEINDICES Summary of this function goes here
% Detailed explanation goes here
indicesOut=zeros(size(indicesIn));
for i=1:size(permutation,1)
currIn=find(indicesIn==permutation(i,1));
indicesOut(currIn)=permutation(i,2);
end
end
and
function [vecOut,allPositions,maxCountPerLabel,currentLabelPositions] = pernuteVector(vecIn,origLabel,newLabel)
%PERNUTEVECTOR Summary of this function goes here
% Detailed explanation goes here
maxNumberLabel=max(origLabel(:));
maxCountPerLabel=[];
allPositions={};
for i=1:maxNumberLabel
allPositions{i}=find(origLabel==i);
maxCountPerLabel(i)=numel(allPositions{i});
end
currentLabelPositions=ones(maxNumberLabel,1);
for j=1:size(origLabel,1)
newLabelVal=newLabel(j);
newLabelPosn=allPositions{newLabelVal};
newLabelPosCurr=newLabelPosn(currentLabelPositions(newLabelVal));
currentLabelPositions(newLabelVal)=mod(currentLabelPositions(newLabelVal),maxCountPerLabel(newLabelVal))+1;
vecOut(j,:)=vecIn(newLabelPosCurr,:);
end
end

Categorie

Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange

Richiesto:

il 30 Ago 2018

Risposto:

il 31 Ago 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by