How to find where indices equal?

6 次查看(过去 30 天)
Zachary Reyes
Zachary Reyes 2018-9-5
编辑: Stephen23 2018-9-5
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]?

回答(1 个)

Stephen23
Stephen23 2018-9-5
编辑:Stephen23 2018-9-5
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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by