How to find where indices equal?

조회 수: 4 (최근 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