Arrays Matlab中无循环矩阵值的变换

Arrays Matlab中无循环矩阵值的变换,arrays,matlab,Arrays,Matlab,我有一个数组,我想根据“索引数组”更改某些列中的值 假设我有一个数组A,其中第1列和第2列中的值将根据下面的矩阵switch\u mat进行切换 A(:,[1 2]) = 1 2 2 6 2 7 6 7 7 12 7 13 12 13 switch_mat = 1 1 2 2 6 3 7 4 12 5 13 6 有没有可能不用循环,使用这样的函数来实

我有一个数组,我想根据“索引数组”更改某些列中的值

假设我有一个数组
A
,其中第1列和第2列中的值将根据下面的矩阵
switch\u mat
进行切换

A(:,[1 2]) =         
 1     2    
 2     6
 2     7
 6     7
 7    12
 7    13
12    13

switch_mat =
 1     1
 2     2
 6     3
 7     4
12     5
13     6
有没有可能不用循环,使用这样的函数来实现这一点

A(:,[1 2]) = renum(A(:,[1 2]),switch_mat)
新的A矩阵将是:

A(:,[1 2]) = 
 1     2    
 2     3
 2     4 
 3     4
 4     5
 4     6
 5     6
1  -> 1
2  -> 2
6  -> 3
7  -> 4
12 -> 5
13 -> 6   % 13 becomes a 6, because they are in the same row of switch_mat
谢谢

编辑: 矩阵A中的开关为:

A(:,[1 2]) = 
 1     2    
 2     3
 2     4 
 3     4
 4     5
 4     6
 5     6
1  -> 1
2  -> 2
6  -> 3
7  -> 4
12 -> 5
13 -> 6   % 13 becomes a 6, because they are in the same row of switch_mat

switch\u mat的尺寸=长度(唯一(A))

以下是一个可能的解决方案:

或者,您可以使用:

我认为后一种方法应该更快