Matlab 使用矩阵元素作为另一个矩阵的索引

Matlab 使用矩阵元素作为另一个矩阵的索引,matlab,Matlab,我有一个0的m×n矩阵,叫做weightmat 我有一个唯一随机整数的m×k矩阵,称为placeIn,其中k

我有一个0的m×n矩阵,叫做
weightmat


我有一个唯一随机整数的m×k矩阵,称为
placeIn
,其中kplaceIn中的最大元素是技巧是
sub2ind

% First generate the row indices used for the indexing.  We'll ignore the column.
[r c] = meshgrid(1:size(placeIn, 2), 1:size(placeIn,1));

weightmat = zeros(5,10);

% Now generate an index for each (c, placeIn) pair, and set the corresponding
% element of weightmat to that value of placeIn
weightmat(sub2ind(size(weightmat), c, placeIn)) = placeIn;

诀窍是
sub2ind

% First generate the row indices used for the indexing.  We'll ignore the column.
[r c] = meshgrid(1:size(placeIn, 2), 1:size(placeIn,1));

weightmat = zeros(5,10);

% Now generate an index for each (c, placeIn) pair, and set the corresponding
% element of weightmat to that value of placeIn
weightmat(sub2ind(size(weightmat), c, placeIn)) = placeIn;

诀窍是
sub2ind

% First generate the row indices used for the indexing.  We'll ignore the column.
[r c] = meshgrid(1:size(placeIn, 2), 1:size(placeIn,1));

weightmat = zeros(5,10);

% Now generate an index for each (c, placeIn) pair, and set the corresponding
% element of weightmat to that value of placeIn
weightmat(sub2ind(size(weightmat), c, placeIn)) = placeIn;

诀窍是
sub2ind

% First generate the row indices used for the indexing.  We'll ignore the column.
[r c] = meshgrid(1:size(placeIn, 2), 1:size(placeIn,1));

weightmat = zeros(5,10);

% Now generate an index for each (c, placeIn) pair, and set the corresponding
% element of weightmat to that value of placeIn
weightmat(sub2ind(size(weightmat), c, placeIn)) = placeIn;