Recently, I need to solve two problems.
-
The first is I have two vectors,
flag
andsrc
. They are the same length.
flag = [0,1,4,0,3,0]
src = [11,9,6,7,0,7]
flag
is a vector whose element is 0 or 1 andsrc
contains some intergers. Then I need to get the vector
flag_ones_idx=[1,2,4]
andsrc_selected_idx=[9,6,0]
.src_selected_idx
is a vector whose elements are in the positions indicated byflag_ones_idx
.
How to perform it? -
Then I need to set an matrix of
M
whose size is10*10
and for most elements are 0s with sparse ones.
Here I need to setM[1,9], M[4,6], M[3,0]
to the value1
,1
,1
(the row index is each element offlag_ones_idx
while the col index is each element ofsrc_selected_idx
). Any ways to do it?