How to find where indices equal?
4 views (last 30 days)
Show older comments
Hello,
I have a 45x3 double. I split those up into three 45x1 doubles. I then used conditions on each of the three 45x1 doubles to get their index, and got out three different sized Nx1 doubles of indexes. My question is how do I extract the indices where all three Nx1 doubles have the same value (index)?
Example: Column 1 = [1,3,4,5] Column 2 = [1,2,3,4,5] Column 4 = [1,3,5]. How would I go about extracting the values into an array which would be [1,3,5]?
0 Comments
Answers (1)
Stephen23
on 5 Sep 2018
Edited: Stephen23
on 5 Sep 2018
This would be much easier if you worked with logical indices rather than subscripts, because then you could just use &, e.g.:
M = your matrix
X = M(:,1)>99 & M(:,2)<0 & isnan(M(:,3))
and you would have your answer already.
But now that you have subscript indices, you can use intersect on each of those index vectors:
X1 = [1,3,4,5];
X2 = [1,2,3,4,5];
X3 = [1,3,5];
intersect(X1,intersect(X2,X3))
You can see how this will get ungainly for more vectors, which is why using logical indexing is much preferred. Note that numbered variables are a sign that you are doing something wrong, and that you should re-think your approach.
0 Comments
See Also
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!