Matlab 用另外两个矩阵索引矩阵

Matlab 用另外两个矩阵索引矩阵,matlab,octave,Matlab,Octave,我有两个行数和列数的大矩阵,还有一个数据矩阵。我想创建一个矩阵,其中: output(i,j) = data(row(i,j),col(i,j)) 我怎样才能很快做到这一点 让[T,N]=大小(行),让[DataT,DataN]=大小(数据),那么一行解决方案是: Soln = reshape(Data(sub2ind([DataT DataN], Row(:), Col(:))), T, N); 这一行程序看起来有点复杂,所以让我们在一个示例中逐步分解它。我加入了一些评论,以说明每个部分的

我有两个行数和列数的大矩阵,还有一个数据矩阵。我想创建一个矩阵,其中:

output(i,j) = data(row(i,j),col(i,j))
我怎样才能很快做到这一点

[T,N]=大小(行)
,让
[DataT,DataN]=大小(数据)
,那么一行解决方案是:

Soln = reshape(Data(sub2ind([DataT DataN], Row(:), Col(:))), T, N);
这一行程序看起来有点复杂,所以让我们在一个示例中逐步分解它。我加入了一些评论,以说明每个部分的情况:

%# Set fixed parameters for example matrices
T = 3; N = 2; 
DataT = 5; DataN = 4;

%# Generate random Data matrix
Data = rand(DataT, DataN);

%# Generate some random subscript index matrices for indexing Data
Row = randi(DataT, T, N);
Col = randi(DataN, T, N);

%# Obtain the linear indices implied by treating Row and Col as subscript matrices
L = sub2ind([DataT DataN], Row(:), Col(:));

%# Use the linear indices to get the data we want
Soln = Data(L);

%# Reshape the data from a vector into matrix of size T by N
Soln = reshape(Soln, T, N);
解决这类问题的标准参考是

可能重复。有一些细微的差别。