Thu 05 Jan 2012 02:42:58 PM UTC, original submission:
For two matrices A and B with the same number of columns, the Matlab function [C,IA,IB] = intersect(A,B,'rows') returns the index vectors IA and IB such that C = A(IA) and C = B(IB). This holds regardless of the number of rows or columns.
Example from Matlab:
>> A=rand(3,4); B=A;
>> [C,IA,IB] = intersect(A,B,'rows')
C =
0.6787 0.3922 0.7060 0.0462
0.7431 0.1712 0.2769 0.8235
0.7577 0.6555 0.0318 0.0971
IA =
1
3
2
IB =
1
3
2
In Octave, however, the intersect function gives an error when requesting more than 1 output argument. The problem appears only when THE NUMBER OF ROWS IS SMALLER THAN THE NUMBER OF COLUMNS. Example:
octave:1> A=rand(3,4); B=A;
octave:2> [C] = intersect(A,B,'rows')
C =
0.180418 0.768069 0.697431 0.823813
0.663449 0.725690 0.470303 0.266596
0.922315 0.395967 0.434297 0.019502
octave:3>
octave:3> [C,IA] = intersect(A,B,'rows')
error: subscript indices must be either positive integers or logicals.
error: called from:
error: /usr/share/octave/3.2.3/m/set/intersect.m at line 76, column 4
octave:3>
octave:3> [C,IA,IB] = intersect(A,B,'rows')
error: subscript indices must be either positive integers or logicals.
error: called from:
error: /usr/share/octave/3.2.3/m/set/intersect.m at line 76, column 4
If the number of rows is LARGER than the number of columns, the intersect function seems to works fine in Octave:
octave:4> A=rand(5,4); B=A;
octave:5> [C,IA,IB] = intersect(A,B,'rows')
C =
0.109814 0.137919 0.947014 0.558020
0.118984 0.778847 0.442949 0.063836
0.373961 0.867630 0.912788 0.841257
0.390284 0.406292 0.661324 0.075581
0.764201 0.560298 0.594685 0.023296
IA =
1
4
5
3
2
IB =
1
4
5
3
2
|