Let be Sa vector with unique elements, and let Sit be a subset of it, also with unique elements; for example, S={1,2,3,4,5,6}and s={1,3,4,6}. Now, given another vector c={7,8,9,7}, how can I create a vector C=[7,0,8,9,0,7], i.e. If S[[i]]is an element of b S, then it C[[i]]is equal to an element of b cwith the same index as S[[i]]in S, otherwise zero.
What I now look like
C=Array[0&,Length[S]];
j=1;
For[i=1,i<=Length[S],i++,If[MemberQ[s,S[[i]]],C[[i]]=c[[j]];j=j+1;]];
This works, but based on the MATLAB background, I hate loops for, and the above operation is a trivial indexing operation in matlab. I am sure there is a smarter way to achieve this la-mathematical style. Anyone have any suggestions?
user564376
source
share