Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 从值向量转换为索引矩阵_Matlab_Matrix_Indexing - Fatal编程技术网

Matlab 从值向量转换为索引矩阵

Matlab 从值向量转换为索引矩阵,matlab,matrix,indexing,Matlab,Matrix,Indexing,我有一个包含值的向量: c = [52, 26, 17, 39, 43]; 以及包含向量的索引的矩阵: B = [1 5 3 2; 4 2 3 1; 2 4 3 1; 4 4 1 2]; 如何将B转换为矩阵B2,其中包含给定B中索引的向量c中的值 B2 = [52 43 17 26; 39 26 17 52; 26 39 17 52; 39 39 52 26]; 嵌套的for循环实现了我想要的: for i = 1:4 f

我有一个包含值的向量:

c = [52, 26, 17, 39, 43];
以及包含向量的索引的矩阵:

B = [1 5 3 2;
     4 2 3 1;
     2 4 3 1;
     4 4 1 2];
如何将B转换为矩阵B2,其中包含给定B中索引的向量c中的值

B2 = [52 43 17 26;
      39 26 17 52;
      26 39 17 52;
      39 39 52 26];
嵌套的for循环实现了我想要的:

for i = 1:4
 for j = 1:4
  B2(i,j) = c(B(i,j));
 end
end

然而,这个问题似乎应该是矢量化的时机成熟了。有没有一种不使用for循环的方法呢?

你可以简单地用
B
索引
c

B2 = c(B);

更好的解决方案+1.不需要重塑。