Matlab:使用逻辑索引将同一向量重复输入矩阵
我希望在特定的(行)逻辑索引处重复输入相同的数字向量到现有矩阵。这就像在所有逻辑索引位置(至少在我的头脑中)只输入一个数字的扩展 也就是说,有可能Matlab:使用逻辑索引将同一向量重复输入矩阵,matlab,matrix,vector,repeat,Matlab,Matrix,Vector,Repeat,我希望在特定的(行)逻辑索引处重复输入相同的数字向量到现有矩阵。这就像在所有逻辑索引位置(至少在我的头脑中)只输入一个数字的扩展 也就是说,有可能 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
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
并获得分配不匹配错误
我希望避免行的for循环或将向量元素分配给单个文件。我有强烈的感觉有一个基本的matlab操作,应该能够做到这一点?谢谢 问题在于,索引从矩阵中选取两行,并尝试将一行分配给它们。您必须复制目标行以适合您的索引:
mat = zeros(5,3);
rowInd = logical([0 1 0 0 1]);
mat(rowInd,:) = repmat([15 6 3],sum(rowInd),1)
这将返回:
mat =
0 0 0
15 6 3
0 0 0
0 0 0
15 6 3
但是
mat
并不总是一个零矩阵?否则:a=[15 6 3],mat=rowInd(:).*a(:)代码>谢谢!我不知怎的认为,一定有一种方法可以做到这一点,类似于单数字的情况,但我现在可以看到,这是一个不同的情况。