Reputation: 241
I have a matrix let it be
A = 100x100 matrix
and a vector
B = [ 2 7 23 45 55 67 79 88 92]
And I want to bring these rows and columns to the end of the array meaning the last 9x9 block in A
to be the rows and columns of B
. (the last row of A
should now be row 92 and the last column should be column 92)
Any ideas?
Upvotes: 2
Views: 628
Reputation: 3898
One approach using ismember
B = [ 2 7 23 45 55 67 79 88 92];
oldIdx = 1:100;
newIdx = [oldIdx(~ismember(oldIdx,B)),B];
out = A(newIdx,newIdx);
Upvotes: 1
Reputation: 25232
One option with setxor
:
A = reshape(1:10000,100,100); %// matrix with linear indices
B = [ 2 7 23 45 55 67 79 88 92]; %// rows and cols to move to the end
idx = [setxor(1:size(A,1),B) B]; %// index vector for rows and cols
out = A(idx,idx)
For the simpler test case of B = [ 1 2 3 4 5 6 7 8 9 ];
you get:
Upvotes: 1
Reputation: 114796
Assuming you do not want to change the order of the rest of the rows/columns, let's start with arranging all the indices:
n = size(A,1);
allIdx = 1:n;
allIdx(B) = []; %// discard B from their original place
allIdx = [allIdx, B]; %// put B at the end
newA = A(allIdx, allIdx); %// Ta-DA!
Upvotes: 3