mardi 26 janvier 2016

Permuting columns of a matrix in MATLAB

Say I have an n by d matrix A and I want to permute the entries of some columns. To do this, I compute permutations of 1 ... n as

idx1 = randperm(n)'
idx2 = randperm(n)'

Then I could do:

A(:,1) = A(idx1,1)
A(:,2) = A(idx2,2)

However, I dont want to do this using a for-loop, as it'll be slow. Say I have an n by d matrix A and an n by d index matrix IDX that specifies the permutations, is there a quicker equivalent of the following for-loop:

for i = 1:d
    A(:,i) = A(IDX(:,i),i);
end




Aucun commentaire:

Enregistrer un commentaire