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)
我想把这些行和列放到数组的末尾表示A中最后的9x9块是b的行和列(A的最后一行应该是第92行,最后一列应该是第92列)
Any ideas?
什么好主意吗?
3 个解决方案
#1
3
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!
#2
1
One option with setxor
:
一个选择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:
对于较简单的B测试用例= [1 2 3 4 5 6 7 8 9];你会得到:
#3
1
One approach using ismember
一种方法使用ismember
B = [ 2 7 23 45 55 67 79 88 92];
oldIdx = 1:100;
newIdx = [oldIdx(~ismember(oldIdx,B)),B];
out = A(newIdx,newIdx);
#1
3
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!
#2
1
One option with setxor
:
一个选择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:
对于较简单的B测试用例= [1 2 3 4 5 6 7 8 9];你会得到:
#3
1
One approach using ismember
一种方法使用ismember
B = [ 2 7 23 45 55 67 79 88 92];
oldIdx = 1:100;
newIdx = [oldIdx(~ismember(oldIdx,B)),B];
out = A(newIdx,newIdx);