如何在 MATLAB 上置换矩阵中的行和列?
在本文中,我们将讨论如何借助多种方法找到矩阵中行和列的排列
方法一
在这种方法中,我们只是按照行和列的指定格式分别置换矩阵的行和列。对于列置换,我们以一个 3*3 矩阵的排列方式为例,它的第一列变成第二列,第二列变成第三列,最后,第三列变成第一列。
示例 1:
Matlab
% MATLAB code for column permutation
% and specifying a 3*3 matrix
A = [1 2 3
4 5 6
7 8 9]
% Initializing a list of columns (Index)
% in which above matrix need to be
% permutted
index = [3 1 2]
% Getting the column permutted matrix B
B = A(:, index)
Matlab
% MATLAB code for rows permutation.
% Specifying a 3*3 matrix
A = [1 2 3
4 5 6
7 8 9]
% Initializing a list of rows (Index)
% in which above matrix need to be
% permutted
index = [3 1 2]
% Getting the rows permutted matrix B
B = A(index, 🙂
Matlab
% MATLAB code for perms()
% Initializing a vector of some elements
vector = [1 2 3];
% Calling the perms() function over the
% above vector as its parameter whose
% elements are going to be permuted
P = perms(vector)
Matlab
% MATLAB code for perms()
% Initializing a vector of some complex numbers
vector = [1+2i 3+4i 5+6i];
% Calling the perms() function over the
% above vector as its parameter whose
% elements are going to be permuted
P = perms(vector)
Matlab
% MATLAB code for permute()
% Creating a random 2*3 matrix
A = rand(2, 3)
% Calling the permute() function
% over the above matrix in the
% dimension order of [2 1]
B = permute(A, [2 1])
Matlab
% MATLAB code for permute ()
% Creating 2-by-3-by-2 random array matrix
A = rand(3, 3, 2)
% Calling the permute() function
% over the above matrix in the
% dimension order of [2 3 1]
B = permute(A, [2 3 1])
输出:
A =
1 2 3
4 5 6
7 8 9
index =
3 1 2
B =
3 1 2
6 4 5
9 7 8
示例 2:
MATLAB
% MATLAB code for rows permutation.
% Specifying a 3*3 matrix
A = [1 2 3
4 5 6
7 8 9]
% Initializing a list of rows (Index)
% in which above matrix need to be
% permutted
index = [3 1 2]
% Getting the rows permutted matrix B
B = A(index, 🙂
输出:
A =
1 2 3
4 5 6
7 8 9
index =
3 1 2
B =
7 8 9
1 2 3
4 5 6
方法二
perms()函数返回一个矩阵,其中包含指定向量“v”中元素的所有可能排列的逆字典序。这里返回矩阵的每一行都包含指定向量“v”的“n”个元素的不同排列。返回的矩阵与给定的向量“v”具有相同的数据类型,并且有 n!行和 n 列。
句法:
perms(v)
参数:此函数接受如下所示的参数:
- v:这是包含“n”个元素的指定向量。
返回值:它返回一个矩阵,其中包含指定向量“v”中元素的所有可能排列的逆字典序。
示例 1:
MATLAB
% MATLAB code for perms()
% Initializing a vector of some elements
vector = [1 2 3];
% Calling the perms() function over the
% above vector as its parameter whose
% elements are going to be permuted
P = perms(vector)
输出:
P =
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
示例 2:
MATLAB
% MATLAB code for perms()
% Initializing a vector of some complex numbers
vector = [1+2i 3+4i 5+6i];
% Calling the perms() function over the
% above vector as its parameter whose
% elements are going to be permuted
P = perms(vector)
输出:
P =
5 + 6i 3 + 4i 1 + 2i
5 + 6i 1 + 2i 3 + 4i
3 + 4i 5 + 6i 1 + 2i
3 + 4i 1 + 2i 5 + 6i
1 + 2i 5 + 6i 3 + 4i
1 + 2i 3 + 4i 5 + 6i
方法三
permute()函数按照向量 diorder 指定的顺序重新排列指定数组的维度。
句法:
permute(A, dimorder)
参数:该函数接受两个参数,如下图所示:
- A:这是指定的数组矩阵。
- dimorder:这是进行排列的指定向量顺序。
返回值:它返回置换后的矩阵。
示例 1:
MATLAB
% MATLAB code for permute()
% Creating a random 2*3 matrix
A = rand(2, 3)
% Calling the permute() function
% over the above matrix in the
% dimension order of [2 1]
B = permute(A, [2 1])
输出:
A =
0.32773 0.12633 0.67752
0.26285 0.91283 0.42994
B =
0.32773 0.26285
0.12633 0.91283
0.67752 0.42994
示例 2:
MATLAB
% MATLAB code for permute ()
% Creating 2-by-3-by-2 random array matrix
A = rand(3, 3, 2)
% Calling the permute() function
% over the above matrix in the
% dimension order of [2 3 1]
B = permute(A, [2 3 1])
输出:
A =
ans(:,:,1) =
0.53364 0.65671 0.32496
0.82471 0.36042 0.31604
0.82714 0.84231 0.70248
ans(:,:,2) =
0.424538 0.498572 0.972245
0.069400 0.799598 0.754885
0.722046 0.807107 0.392804
B =
ans(:,:,1) =
0.53364 0.42454
0.65671 0.49857
0.32496 0.97224
ans(:,:,2) =
0.824706 0.069400
0.360418 0.799598
0.316038 0.754885
ans(:,:,3) =
0.82714 0.72205
0.84231 0.80711
0.70248 0.39280