Thread Subject: Get line index on matrix

Subject: Get line index on matrix

From: Joe Nunes

Date: 5 Nov, 2009 11:46:02

Message: 1 of 12

Hello,

I'm trying to get the line index of a nx3 matrix for a given vector (1x3), like

0.1 0.3 1.6
0.2 0.7 1.2
1.3 0.1 7.3
(...)

searching for vector = 0.2 0.7 1.2 should return index = 2.

i've tried find() and a script which compares

    if(vector(i,:) == points(i,:))
        index = i;

inside a for() loop (but it says that the condition is not valid, on debug)

I would appreciate your help.

Best regards

Subject: Get line index on matrix

From: Wayne King

Date: 5 Nov, 2009 12:03:02

Message: 2 of 12

"Joe Nunes" <vazdepaivanunes@gmail.com> wrote in message <hcudtq$phk$1@fred.mathworks.com>...
> Hello,
>
> I'm trying to get the line index of a nx3 matrix for a given vector (1x3), like
>
> 0.1 0.3 1.6
> 0.2 0.7 1.2
> 1.3 0.1 7.3
> (...)
>
> searching for vector = 0.2 0.7 1.2 should return index = 2.
>
> i've tried find() and a script which compares
>
> if(vector(i,:) == points(i,:))
> index = i;
>
> inside a for() loop (but it says that the condition is not valid, on debug)
>
> I would appreciate your help.
>
> Best regards

Hi Joe, will the following work for you?

A=[0.1 0.3 1.6
0.2 0.7 1.2
1.3 0.1 7.3];
B= [0.2 0.7 1.2];
[C,IA] = intersect(A,B,'rows');
% IA is equal to 2
IA

Wayne

Subject: Get line index on matrix

From: Joe Nunes

Date: 5 Nov, 2009 12:12:01

Message: 3 of 12

"Wayne King" <wmkingty@gmail.com> wrote in message <hcuetm$6t$1@fred.mathworks.com>...
> "Joe Nunes" <vazdepaivanunes@gmail.com> wrote in message <hcudtq$phk$1@fred.mathworks.com>...
> > Hello,
> >
> > I'm trying to get the line index of a nx3 matrix for a given vector (1x3), like
> >
> > 0.1 0.3 1.6
> > 0.2 0.7 1.2
> > 1.3 0.1 7.3
> > (...)
> >
> > searching for vector = 0.2 0.7 1.2 should return index = 2.
> >
> > i've tried find() and a script which compares
> >
> > if(vector(i,:) == points(i,:))
> > index = i;
> >
> > inside a for() loop (but it says that the condition is not valid, on debug)
> >
> > I would appreciate your help.
> >
> > Best regards
>
> Hi Joe, will the following work for you?
>
> A=[0.1 0.3 1.6
> 0.2 0.7 1.2
> 1.3 0.1 7.3];
> B= [0.2 0.7 1.2];
> [C,IA] = intersect(A,B,'rows');
> % IA is equal to 2
> IA
>
> Wayne

Yes, i've tried, but IA returns an Empty matrix: 0-by-1 .. Since my set of points is 1312x3 and the points to find are in 17x3, i've tried
[C,IA] = intersect(points,keypoints(2,:),'rows')

but it keeps returning empty matrix. Is this because of rounding or something ? i don't understand. This points from 17x3 matrix were taken from the original set.

Subject: Get line index on matrix

From: Jos

Date: 5 Nov, 2009 12:19:02

Message: 4 of 12

"Wayne King" <wmkingty@gmail.com> wrote in message <hcuetm$6t$1@fred.mathworks.com>...
> "Joe Nunes" <vazdepaivanunes@gmail.com> wrote in message <hcudtq$phk$1@fred.mathworks.com>...
> > Hello,
> >
> > I'm trying to get the line index of a nx3 matrix for a given vector (1x3), like
> >
> > 0.1 0.3 1.6
> > 0.2 0.7 1.2
> > 1.3 0.1 7.3
> > (...)
> >
> > searching for vector = 0.2 0.7 1.2 should return index = 2.
> >
> > i've tried find() and a script which compares
> >
> > if(vector(i,:) == points(i,:))
> > index = i;
> >
> > inside a for() loop (but it says that the condition is not valid, on debug)
> >
> > I would appreciate your help.
> >
> > Best regards
>
> Hi Joe, will the following work for you?
>
> A=[0.1 0.3 1.6
> 0.2 0.7 1.2
> 1.3 0.1 7.3];
> B= [0.2 0.7 1.2];
> [C,IA] = intersect(A,B,'rows');
> % IA is equal to 2
> IA
>
> Wayne

or take a look at ISMEMBER or even more directly BSXFUN (in combination with ALL)

A=[0.1 0.3 1.6 ; 0.2 0.7 1.2 ; 1.3 0.1 7.3]; B= [0.2 0.7 1.2];
t1 = bsxfun(@eq,A,B)
t2 = all(t1,2)
r = find(t2)

By the way, be quite careful when comparing floating points:

a = 0.1+0.2+0.3, b = 0.6, a==b

hth
Jos

Subject: Get line index on matrix

From: Wayne King

Date: 5 Nov, 2009 12:28:01

Message: 5 of 12

"Joe Nunes" <vazdepaivanunes@gmail.com> wrote in message <hcufeh$298$1@fred.mathworks.com>...
> "Wayne King" <wmkingty@gmail.com> wrote in message <hcuetm$6t$1@fred.mathworks.com>...
> > "Joe Nunes" <vazdepaivanunes@gmail.com> wrote in message <hcudtq$phk$1@fred.mathworks.com>...
> > > Hello,
> > >
> > > I'm trying to get the line index of a nx3 matrix for a given vector (1x3), like
> > >
> > > 0.1 0.3 1.6
> > > 0.2 0.7 1.2
> > > 1.3 0.1 7.3
> > > (...)
> > >
> > > searching for vector = 0.2 0.7 1.2 should return index = 2.
> > >
> > > i've tried find() and a script which compares
> > >
> > > if(vector(i,:) == points(i,:))
> > > index = i;
> > >
> > > inside a for() loop (but it says that the condition is not valid, on debug)
> > >
> > > I would appreciate your help.
> > >
> > > Best regards
> >
> > Hi Joe, will the following work for you?
> >
> > A=[0.1 0.3 1.6
> > 0.2 0.7 1.2
> > 1.3 0.1 7.3];
> > B= [0.2 0.7 1.2];
> > [C,IA] = intersect(A,B,'rows');
> > % IA is equal to 2
> > IA
> >
> > Wayne
>
> Yes, i've tried, but IA returns an Empty matrix: 0-by-1 .. Since my set of points is 1312x3 and the points to find are in 17x3, i've tried
> [C,IA] = intersect(points,keypoints(2,:),'rows')
>
> but it keeps returning empty matrix. Is this because of rounding or something ? i don't understand. This points from 17x3 matrix were taken from the original set.

Hi, Maybe.. are you doing this inside of an M-file? If you do it in the command window (outside of an M-file) does it work? How about if you force them both to be doubles?

s = RandStream('mt19937ar');
RandStream.setDefaultStream(s);
A = randn(1000,3);
 B=A(4,:);
 [C,IA] = intersect(double(A),double(B),'rows');
% IA is 4
IA

wayne

Subject: Get line index on matrix

From: Joe Nunes

Date: 5 Nov, 2009 15:04:02

Message: 6 of 12

i've tried forcing to double. that didn't work either. i don't have the bsxfun function, i've looked to ismember though .. =(

"Wayne King" <wmkingty@gmail.com> wrote in message <hcugch$fv$1@fred.mathworks.com>...
> "Joe Nunes" <vazdepaivanunes@gmail.com> wrote in message <hcufeh$298$1@fred.mathworks.com>...
> > "Wayne King" <wmkingty@gmail.com> wrote in message <hcuetm$6t$1@fred.mathworks.com>...
> > > "Joe Nunes" <vazdepaivanunes@gmail.com> wrote in message <hcudtq$phk$1@fred.mathworks.com>...
> > > > Hello,
> > > >
> > > > I'm trying to get the line index of a nx3 matrix for a given vector (1x3), like
> > > >
> > > > 0.1 0.3 1.6
> > > > 0.2 0.7 1.2
> > > > 1.3 0.1 7.3
> > > > (...)
> > > >
> > > > searching for vector = 0.2 0.7 1.2 should return index = 2.
> > > >
> > > > i've tried find() and a script which compares
> > > >
> > > > if(vector(i,:) == points(i,:))
> > > > index = i;
> > > >
> > > > inside a for() loop (but it says that the condition is not valid, on debug)
> > > >
> > > > I would appreciate your help.
> > > >
> > > > Best regards
> > >
> > > Hi Joe, will the following work for you?
> > >
> > > A=[0.1 0.3 1.6
> > > 0.2 0.7 1.2
> > > 1.3 0.1 7.3];
> > > B= [0.2 0.7 1.2];
> > > [C,IA] = intersect(A,B,'rows');
> > > % IA is equal to 2
> > > IA
> > >
> > > Wayne
> >
> > Yes, i've tried, but IA returns an Empty matrix: 0-by-1 .. Since my set of points is 1312x3 and the points to find are in 17x3, i've tried
> > [C,IA] = intersect(points,keypoints(2,:),'rows')
> >
> > but it keeps returning empty matrix. Is this because of rounding or something ? i don't understand. This points from 17x3 matrix were taken from the original set.
>
> Hi, Maybe.. are you doing this inside of an M-file? If you do it in the command window (outside of an M-file) does it work? How about if you force them both to be doubles?
>
> s = RandStream('mt19937ar');
> RandStream.setDefaultStream(s);
> A = randn(1000,3);
> B=A(4,:);
> [C,IA] = intersect(double(A),double(B),'rows');
> % IA is 4
> IA
>
> wayne

Subject: Get line index on matrix

From: ImageAnalyst

Date: 5 Nov, 2009 15:07:01

Message: 7 of 12

Are you aware of section 6.1 of the FAQ, which relates to comparing
floating point numbers for exact equality?
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Get line index on matrix

From: Jos

Date: 5 Nov, 2009 15:17:03

Message: 8 of 12

"Joe Nunes" <vazdepaivanunes@gmail.com> wrote in message <hcuph2$2mp$1@fred.mathworks.com>...
> i've tried forcing to double. that didn't work either. i don't have the bsxfun function, i've looked to ismember though .. =(

So, what did you look at? Did you try anything?

Jos

Subject: Get line index on matrix

From: dpb

Date: 5 Nov, 2009 15:20:34

Message: 9 of 12

Joe Nunes wrote:
...
> Yes, i've tried, but IA returns an Empty matrix: 0-by-1 .. Since my
> set of points is 1312x3 and the points to find are in 17x3, i've
> tried [C,IA] = intersect(points,keypoints(2,:),'rows')
>
> but it keeps returning empty matrix. Is this because of rounding or
> something ? i don't understand. This points from 17x3 matrix were
> taken from the original set.

Well, could well be/probably is--all it takes is the last bit of the
mantissa to be different for the comparison to fail on equality.

You might look at comparing the individual values and outputting that
difference/equality result to see where the actual failure is occurring.
  If you're doing any computation or i/o or other operations between
there are chances for rounding to occur.

But, in general, for robust coding you'll need to do some sort of
tolerancing on the interval for selection of floating point values or
some case will inevitably come back to bite even if you figure out a
workaround for this specific set of values.

Somebody else noted the FAQ.

--

Subject: Get line index on matrix

From: Bruno Luong

Date: 6 Nov, 2009 07:27:02

Message: 10 of 12

Replace ISMEMBER call with ISMEMBERF on FEX:

http://www.mathworks.com/matlabcentral/fileexchange/23294-ismemberf

Bruno

Subject: Get line index on matrix

From: Bruno Luong

Date: 6 Nov, 2009 07:39:02

Message: 11 of 12

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hd0j46$n1i$1@fred.mathworks.com>...
> Replace ISMEMBER call with ISMEMBERF on FEX:
>
> http://www.mathworks.com/matlabcentral/fileexchange/23294-ismemberf
>
> Bruno

Example:

>> vector=[1 3 16;
                  2 7 12;
                  13 1 73]*(1/10)

vector =

    0.1000 0.3000 1.6000
    0.2000 0.7000 1.2000
    1.3000 0.1000 7.3000

>> [t idx]=ismember([0.2 0.7 1.2],vector,'rows') % <- exact comparison

t =

     0


idx =

     0

>> [t idx]=ismemberf([0.2 0.7 1.2],vector,'rows')

t =

     1


idx =

     2

% Bruno

Subject: Get line index on matrix

From: Joe Nunes

Date: 6 Nov, 2009 10:49:02

Message: 12 of 12

It worked!!!!! thank you very much!

Best Regards

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hd0jqm$67p$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hd0j46$n1i$1@fred.mathworks.com>...
> > Replace ISMEMBER call with ISMEMBERF on FEX:
> >
> > http://www.mathworks.com/matlabcentral/fileexchange/23294-ismemberf
> >
> > Bruno
>
> Example:
>
> >> vector=[1 3 16;
> 2 7 12;
> 13 1 73]*(1/10)
>
> vector =
>
> 0.1000 0.3000 1.6000
> 0.2000 0.7000 1.2000
> 1.3000 0.1000 7.3000
>
> >> [t idx]=ismember([0.2 0.7 1.2],vector,'rows') % <- exact comparison
>
> t =
>
> 0
>
>
> idx =
>
> 0
>
> >> [t idx]=ismemberf([0.2 0.7 1.2],vector,'rows')
>
> t =
>
> 1
>
>
> idx =
>
> 2
>
> % Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix index ve... Joe Nunes 5 Nov, 2009 06:49:05
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com