📜  如何在MATLAB中交换矩阵中的元素?

📅  最后修改于: 2022-05-13 01:55:44.540000             🧑  作者: Mango

如何在MATLAB中交换矩阵中的元素?

在本文中,我们将看到在 MATLAB 中将元素交换为矩阵。不同的方法如下图所示:

方法一:通过改变行和列的元素

 在这种方法中,我们只是分别更改指定行和列中特定行和列的元素。

示例 1:

Matlab
% MATLAB code for 2*2 matrix. its first and
% second elements of the first column are being swapped
A = [5  10
     15 20]
       
% Swapping the first and second elements of the first column
A([1 2]) = A([2 1])


Matlab
% MATLAB code for 3*3 matrix. The second and third elements of the first 
% column are being swapped. And later, the first and second elements 
% of the second column of the swapped matrix are swapped again.
A = [5  10 15 
     20 25 30
     35 40 45]
       
% Swapping the second and third elements of the first column
A([2 3]) = A([3 2])
  
% Swapping the first and second elements of the second column
% of the above swapped matrix
A([4 5]) = A([5 4])


Matlab
% MATLAB code for swapping element
% of the array row-wise
% Initializing an array
A = [1 2 3
     4 5 6
     7 8 9];
  
% Calling the randperm() function with
% size() as its paramter
random = A(randperm(size(A, 1)),:)


Matlab
% MATLAB code for swapping elements
% of the array column-wise
% Initializing an aary
A = [1 2 3
     4 5 6
     7 8 9];
  
% Calling the randperm() function with
% size() as its paramter
random = A(:, randperm(size(A, 1)))


输出:



A =
   5   10
  15   20
A =
  15   10
   5   20

示例 2:

MATLAB

% MATLAB code for 3*3 matrix. The second and third elements of the first 
% column are being swapped. And later, the first and second elements 
% of the second column of the swapped matrix are swapped again.
A = [5  10 15 
     20 25 30
     35 40 45]
       
% Swapping the second and third elements of the first column
A([2 3]) = A([3 2])
  
% Swapping the first and second elements of the second column
% of the above swapped matrix
A([4 5]) = A([5 4])

输出:

A =
   5   10   15
  20   25   30
  35   40   45
A =
   5   10   15
  35   25   30
  20   40   45
A =
   5   25   15
  35   10   30
  20   40   45

方法2:通过使用 randperm() 和 size() 函数

 在这种方法中,我们使用了randperm()size()函数的组合。

randperm()

randperm()函数用于指定矩阵的整数的随机排列。

参数:该函数接受一个参数。



  • A:这是指定的矩阵。

尺寸()

size()函数用于返回指定数组“X”的每个维度的大小或指定矩阵“X”的大小。

这里,

size(X)返回具有 ndims(X) 元素的向量 d 中指定数组“X”的每个维度的大小。

[m,n] = size(X)在单独的变量 m 和 n 中返回指定矩阵“X”的大小。

size(X,dim)返回由标量 dim 指定的“X”维度的大小。



[d1,d2,d3,...,dn] = size(X)返回单独变量中指定数组“X”的前 n 个维度的大小。

参数:该函数接受两个参数,如下图所示:

  • X:它是指定的数组或矩阵或维度。
  • dim:它是指定维度“X”的标量值

示例 1:

MATLAB

% MATLAB code for swapping element
% of the array row-wise
% Initializing an array
A = [1 2 3
     4 5 6
     7 8 9];
  
% Calling the randperm() function with
% size() as its paramter
random = A(randperm(size(A, 1)),:)

输出:

random =
  7   8   9
  1   2   3
  4   5   6

示例 2:

MATLAB

% MATLAB code for swapping elements
% of the array column-wise
% Initializing an aary
A = [1 2 3
     4 5 6
     7 8 9];
  
% Calling the randperm() function with
% size() as its paramter
random = A(:, randperm(size(A, 1)))

输出:

random =
  3   1   2
  6   4   5
  9   7   8