1
votes

I would like to enter the same vector of numbers repeatedly to an existing matrix at specific (row) logical indices. This is like an extension of entering just a single number at all logical index positions (at least in my head).

I.e., it is possible to have

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

But I would like to do sth like this

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

and get an assignment mismatch error.

I want to avoid for loops for the rows or assigning vector elements single file. I have the strong feeling there is an elementary matlab operation that should be able to do this? Thanks!

1
but mat is not always a zero matrix? otherwise: a = [15 6 3], mat = rowInd(:).*a(:).'; - Robert Seifert

1 Answers

2
votes

The problem is that your indexing picks two rows from the matrix and tries to assign a single row to them. You have to replicate the targeted row to fit your indexing:

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

This returns:

mat =

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