help with find() or with logical indexing
Show older comments
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.
6 Comments
MSP
on 29 Jul 2017
According to the question aren't you supposed to get idx1=[10 11 12] when j=4
William Rose
on 29 Jul 2017
Image Analyst
on 30 Jul 2017
Well if idx1 = [2,3,4], it's true that S(idx1) will equal 1. HOWEVER, if idx1 = [2,3,4,6] then S(idx1) will ALSO equal 1 for each value of idx1. What if idx1 = [2,3,4,6] - the first 4 values, which will return the first 4 values of S which are all 1. Why do you want idx1 = [2,3,4] instead of [2,3,4,6]?
And when you say that idx1 = 6, then S(idx1) equals S(6) which is 2, which is j for that case, but with a j of 2, it would also work to have idx1 = 5, because S(5) = 2. Why is idx1 not equal to [5,6]?
Walter Roberson
on 30 Jul 2017
"According to the question aren't you supposed to get idx1=[10 11 12] when j=4"
No, only index values in idx can be returned, and 12 is not in idx.
Walter Roberson
on 30 Jul 2017
Edited: Walter Roberson
on 30 Jul 2017
I just tested the Answer I posted a couple of hours ago, and it produced exactly the desired output...
William Rose
on 30 Jul 2017
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!