# Thread Subject: tricky table lookup / comparison problem

 Subject: tricky table lookup / comparison problem From: AJP Date: 21 May, 2011 19:46:04 Message: 1 of 6 Say I have two matrices A and B: A = 1 1 2 1 2 2 1 3 2 B = 1 1 1 1 1 3 1 2 1 1 2 3 1 3 1 1 3 3 The first two columns in both A and B matrix describe, say, (x,y) coordinates. The third column in A describes, say, the "true value" of some parameter at that location. The third column in B describes a range of estimates of the parameter corresponding to the given (x,y) location. For each unique (x,y) in A, I want to test the column 3 elements in B to see whether they are above or below the true value in the third column of A. I then want to return a column vector of logicals length(B) with 0 if it is below and 1 if it is above. For the given A and B, a successful procedure would return: result = 0 1 0 1 0 1 Note that the procedure cannot rely on nicely ordered A and B matrices. I have tried using a few set operations but these aren't helping as I have to get the logical test in there somewhere. Does anyone have any ideas on how to approah this sort of problem? Many thanks to anyone who can shed any light on it.
 Subject: tricky table lookup / comparison problem From: Roger Stafford Date: 21 May, 2011 20:27:02 Message: 2 of 6 "AJP" wrote in message ... > ....... > For each unique (x,y) in A, I want to test the column 3 elements in B to see whether they are above or below the true value in the third column of A. I then want to return a column vector of logicals length(B) with 0 if it is below and 1 if it is above. ..... > ....... - - - - - - - - - -  [~,loc] = ismember(B(:,1:2),A(:,1:2));  T = B(:,3) > A(loc,3); % <-- This is your logical vector This assumes that every pair in the first two columns of B will be found somewhere in those of A, and also that every pair in A is unique. Roger Stafford
 Subject: tricky table lookup / comparison problem From: AJP Date: 21 May, 2011 20:45:05 Message: 3 of 6 "Roger Stafford" wrote in message ... > "AJP" wrote in message ... > > ....... > > For each unique (x,y) in A, I want to test the column 3 elements in B to see whether they are above or below the true value in the third column of A. I then want to return a column vector of logicals length(B) with 0 if it is below and 1 if it is above. ..... > > ....... > - - - - - - - - - - > [~,loc] = ismember(B(:,1:2),A(:,1:2)); > T = B(:,3) > A(loc,3); % <-- This is your logical vector > > This assumes that every pair in the first two columns of B will be found somewhere in those of A, and also that every pair in A is unique. > > Roger Stafford Thanks for your input Roger. "loc" appears to be a 2xlength(B) matrix, therefore A(loc,3) does not work. Maybe a small correction is required? Here's what I had come up with so far using a for loop (sort of in debug mode, returning lots of checkabe stuff...) for n=1:length(A) ind1=ismember(B(:,1:2),A(:,1:2),'rows') B(ind1,:) ind2=B(ind1,3)>A(n,3) end ind1 is a column vector length B and identifies the rows where there is a match in the coordinates. ind2 is a column vector of length 2 (in this case) and identifies which of these rows are abve or below the true value. Now if I can just unite the two pieces of info in ind1 and ind2 I'll have done it. But I don't know how to do it! :D
 Subject: tricky table lookup / comparison problem From: AJP Date: 21 May, 2011 20:52:05 Message: 4 of 6 Sorry, I meant to say "loc" appears to be a length(B) x 2 matrix, not a 2 x length(B) matrix.
 Subject: tricky table lookup / comparison problem From: AJP Date: 21 May, 2011 20:59:05 Message: 5 of 6 Roger, I have seen your very small error. you wrote: [~,loc] = ismember(B(:,1:2),A(:,1:2)); T = B(:,3) > A(loc,3); % <-- This is your logical vector when you meant: [~,loc] = ismember(B(:,1:2),A(:,1:2),'rows'); T = B(:,3) > A(loc,3); (i.e. you missed out the 'rows' keyword). But thank you very much for your help - problem solved and very neatly too! Kind regards, Adam
 Subject: tricky table lookup / comparison problem From: Roger Stafford Date: 22 May, 2011 00:07:02 Message: 6 of 6 "AJP" wrote in message ... > when you meant: > [~,loc] = ismember(B(:,1:2),A(:,1:2),'rows'); > ...... - - - - - - - -   Yes, that was careless of me. I had written the 'rows' option on scratch paper but it somehow didn't make it into the formal reply. I'm afraid my age is catching up with me. Roger Stafford

### Everyone's Tags:

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 Applied By Date/Time
lookup AJP 21 May, 2011 15:49:10
lookup table AJP 21 May, 2011 15:49:10
comparison AJP 21 May, 2011 15:49:10
set AJP 21 May, 2011 15:49:10
ismember AJP 21 May, 2011 15:49:10
logical test AJP 21 May, 2011 15:49:10
test AJP 21 May, 2011 15:49:10
logical AJP 21 May, 2011 15:49:10