MATLAB: Help with find() or with logical indexing

findlogical indexing

I want to find array idx1, which is the values of array idx such that S(idx1) == j.
Example: Suppose
idx=[2 3 4 6 7 9 10 11], S=[1 1 1 1 2 2 3 3 3 4 4 4 4]
When j=1, I want idx1=[2 3 4];
when j=2, I want idx1=[6];
when j=3, I want idx1=[7 9];
when j=4, I want idx1=[10 11].
I tried idx1=idx(S==j)) and other ideas, but have not found the answer.
Thank you.

Best Answer

intersect( find(S == j), idx )