Recently, I need to solve two problems.


The first is I have two vectors, flag and src. 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 and src contains some intergers.  Then I need to get the vector
flag_ones_idx=[1,2,4] and src_selected_idx=[9,6,0]. src_selected_idx is a vector whose elements are in the positions indicated by flag_ones_idx.
How to perform it?


Then I need to set an matrix of M whose size is 10*10 and for most elements are 0s with sparse ones.
Here I need to set M[1,9], M[4,6], M[3,0] to the value 1,1,1(the row index is each element of flag_ones_idx while the col index is each element of src_selected_idx). Any ways to do it?