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

问题描述 投票:1回答:1

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

即,有可能

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操作,应该能够做到这一点?谢谢!

matlab matrix vector repeat
1个回答
2
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.