How to find where indices equal?

1 vue (au cours des 30 derniers jours)
Zachary Reyes
Zachary Reyes le 5 Sep 2018
Modifié(e) : Stephen23 le 5 Sep 2018
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]?

Réponses (1)

Stephen23
Stephen23 le 5 Sep 2018
Modifié(e) : Stephen23 le 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.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits


Version

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by