Matlab:使用逻辑索引重复输入相同的向量到矩阵

Matlab: enter same vector repeatedly to matrix using logical indexing

我想在特定(行)逻辑索引的现有矩阵中重复输入相同的数字向量。这就像在所有逻辑索引位置(至少在我的脑海中)只输入一个数字的扩展。

也就是说,有可能

mat    = zeros(5,3);
rowInd = logical([0 1 0 0 1]); %normally obtained from previous operation

mat(rowInd,1) = 15; 
mat =

     0     0     0
    15     0     0
     0     0     0
     0     0     0
    15     0     0

但我想这样做

mat(rowInd,:) = [15 6 3]; %rows 2 and 5 should be filled with these numbers

并收到分配不匹配错误。

我想避免对行进行循环或为单个文件分配矢量元素。我有一种强烈的感觉,有一个基本的 matlab 操作应该能够做到这一点?谢谢!

问题是您的索引从矩阵中选取了两行并尝试为它们分配一行。您必须复制目标行以适合您的索引:

mat = zeros(5,3);
rowInd = logical([0 1 0 0 1]);
mat(rowInd,:) = repmat([15 6 3],sum(rowInd),1)

这个returns:

mat =

     0     0     0
    15     6     3
     0     0     0
     0     0     0
    15     6     3