| Contents | Index |
In a future release, the behavior of intersect will change. This change is introduced for adoption in R2012a. For a detailed demonstration that shows how you can preview the future behavior and preserve the current behavior of your existing code, see the example, Assessing the Impact of Forthcoming Changes to intersect. For a detailed explanation of all the forthcoming changes, see Set Functions Changing Behavior in a Future Release.
C = intersect(A,B,'rows') returns the rows common to both A and B. The rows of the matrix C are in sorted order.
[C,ia,ib] = intersect(___,'R2012a') adopts the future behavior of the intersect function. [C,ia,ib] = intersect(___,'legacy') preserves the current behavior. The ___ symbol signifies that you can specify either flag, 'R2012a' or 'legacy', as the final argument with any previous syntax that accepts A, B, or 'rows'.
A = [7 1 7 7 4]; B = [7 0 4 4 0]; C = intersect(A,B)
C =
4 7A = [7 1 7 7 4]; B = [7 0 4 4 0]; [C,ia,ib] = intersect(A,B)
C =
4 7
ia =
5 4
ib =
4 1A = [2 2 2; 0 0 1; 1 2 3; 1 1 1];
B = [1 2 3; 2 2 2; 2 2 0];
[C,ia,ib] = intersect(A,B,'rows')C =
1 2 3
2 2 2
ia =
3
1
ib =
1
2A and B do not need to have the same number of rows, but they must have the same number of columns.
Use the setOrder argument to specify the ordering of the values in C.
Specify 'stable' if you want the values in C to have the same order as in A.
A = [7 1 7 7 4]; B = [7 0 4 4 0];
[C,ia,ib] = intersect(A,B,'stable')C =
7 4
ia =
1
5
ib =
1
3Alternatively, you can specify 'sorted' order.
[C,ia,ib] = intersect(A,B,'sorted')C =
4 7
ia =
5
1
ib =
3
1intersect adopts the forthcoming behavior when you specify the setOrder argument. If you omit the setOrder argument, the output is sorted, but the size and content of C, ia, and ib conforms to the current behavior.
Use the 'R2012a' flag to assess the impact of the forthcoming behavior changes. Use the 'legacy' flag to preserve the current behavior of your existing code.
Find the intersection of A and B with the current default behavior.
A = [7 1 7 7 4]; B = [7 0 4 4 0]; [C1,ia1,ib1] = intersect(A,B)
C1 =
4 7
ia1 =
5 4
ib1 =
4 1
Find the intersection of A and B, and opt into the forthcoming behavior. In the future, this behavior will be the default.
[C2,ia2,ib2] = intersect(A,B,'R2012a')C2 =
4 7
ia2 =
5
1
ib2 =
3
1Notice that ia1 and ia2 have different shapes and content. The same is also true for ib1 and ib2.
Find the unique elements of A, and preserve the current behavior.
[C3,ia3,ib3] = intersect(A,B,'legacy')C3 =
4 7
ia3 =
5 4
ib3 =
4 1Notice that C3, ia3, and ib3 match C1, ia1, and ib1 respectively.
A = [5 NaN NaN]; B = [5 NaN NaN]; C = intersect(A,B)
C =
5
intersect treats NaN values as distinct.
C | Values Common to A and B Values common to both A and B, returned as a vector or matrix. C is a vector unless you specify the 'rows' flag. If 'rows' is specified, C is a matrix containing the rows common to both A and B. |
ia | Index to A
Index vector that identifies the elements in A that are common to B. If there is a repeated value (or row) in A, then ia contains the index to the last occurrence of the value (or row).
If you call intersect with the setOrder or 'R2012a' argument, ia contains the index to the first occurrence of any repeated value (or row) in A. |
ib | Index to B
Index vector that identifies the elements in B that are common to A. If there is a repeated value (or row) in B, then ib contains the index to the last occurrence of the value (or row).
If you call intersect with the setOrder or 'R2012a' argument, ib contains the index to the first occurrence of any repeated value (or row) in B. |
ismember | issorted | setdiff | setxor | sort | union | unique

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |