Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
comparing the elements of two matrix

Subject: comparing the elements of two matrix

From: SM

Date: 17 Feb, 2011 14:30:07

Message: 1 of 10

Hi all,
I have matrix A with dimension nX2 and I have matrix B with dimension mX2 which contains mt threshold value(first column is the lower threshold and the second column is upper threshold).

I want to compare the values of the first column in A matrix with the thresholds in matrix B, and keep the corresponding value in column 3for the element of the second column which fits between any set of the thresholds level. I tried to use "bsxfun", but couldn't manage it since my in my matrix B, each row (set) contains two values...
For instance if
A= [ 1 4;...
       2 6;...
       5 3;...
       12 1;...
       11 7;...
       9 11;...
       6 10];

B= [1 3;...
       6 8;...
      12 17];

then I want the output as:
C= [1 4;...
      2 6;...
      5 0;...
     12 1;...
     11 0;...
      9 0;...
     12 17];
thanks in advance

Subject: comparing the elements of two matrix

From: Sean de

Date: 17 Feb, 2011 14:44:04

Message: 2 of 10

"SM" wrote in message <ijjbdf$qdt$1@fred.mathworks.com>...
> Hi all,
> I have matrix A with dimension nX2 and I have matrix B with dimension mX2 which contains mt threshold value(first column is the lower threshold and the second column is upper threshold).
>
> I want to compare the values of the first column in A matrix with the thresholds in matrix B, and keep the corresponding value in column 3for the element of the second column which fits between any set of the thresholds level. I tried to use "bsxfun", but couldn't manage it since my in my matrix B, each row (set) contains two values...

What 3rd column? They are nx2

Subject: comparing the elements of two matrix

From: SM

Date: 17 Feb, 2011 15:06:03

Message: 3 of 10


Sorry I meant the values of second column in matrix A...

Subject: comparing the elements of two matrix

From: Bruno Luong

Date: 17 Feb, 2011 19:18:04

Message: 4 of 10

"SM" wrote in message <ijjbdf$qdt$1@fred.mathworks.com>...
> Hi all,
> I have matrix A with dimension nX2 and I have matrix B with dimension mX2 which contains mt threshold value(first column is the lower threshold and the second column is upper threshold).
>
> I want to compare the values of the first column in A matrix with the thresholds in matrix B, and keep the corresponding value in column 3for the element of the second column which fits between any set of the thresholds level. I tried to use "bsxfun", but couldn't manage it since my in my matrix B, each row (set) contains two values...
> For instance if
> A= [ 1 4;...
> 2 6;...
> 5 3;...
> 12 1;...
> 11 7;...
> 9 11;...
> 6 10];
>
> B= [1 3;...
> 6 8;...
> 12 17];
>

C = A(~any(bsxfun(@ge,A(:,1),B(:,1)') & bsxfun(@le,A(:,1),B(:,2)'),2),2)=0

Bruno

Subject: comparing the elements of two matrix

From: Bruno Luong

Date: 17 Feb, 2011 21:07:03

Message: 5 of 10

Opps, sorry for a faulty code:

C = A;
C(~any(bsxfun(@ge,A(:,1),B(:,1)') & bsxfun(@le,A(:,1),B(:,2)'),2),2)=0

Bruno

Subject: comparing the elements of two matrix

From: SM

Date: 18 Feb, 2011 16:12:03

Message: 6 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ijk2ln$n32$1@fred.mathworks.com>...
> Opps, sorry for a faulty code:
>
> C = A;
> C(~any(bsxfun(@ge,A(:,1),B(:,1)') & bsxfun(@le,A(:,1),B(:,2)'),2),2)=0
>
> Bruno

Thanks Bruno, I already fix that one...but faced another Problem. Now I am trying to replace the element of the A(:,1) with the mean value of the average value of lower and upper threshold which I add it as a third column in matrix B if it meets the same critrion...but it gives error...I even tried doing that using for loop but I didn't manage...any idea how I can make that???

Subject: comparing the elements of two matrix

From: SM

Date: 18 Feb, 2011 17:41:05

Message: 7 of 10

I guess I got it myself and hope its the correct one:

for i=1:length(A)
for j=1:length(B)
if bsxfun(@ge,A(i,1),B(j,1)') & bsxfun(@le,A(i,1),B(j,2)')
A(i,1)=B(j,3);
end
end
end

thanks again for your help

Subject: comparing the elements of two matrix

From: Bruno Luong

Date: 18 Feb, 2011 19:17:07

Message: 8 of 10

"SM" wrote in message <ijmavh$sc9$1@fred.mathworks.com>...
> I guess I got it myself and hope its the correct one:
>
> for i=1:length(A)
> for j=1:length(B)
> if bsxfun(@ge,A(i,1),B(j,1)') & bsxfun(@le,A(i,1),B(j,2)')
> A(i,1)=B(j,3);
> end
> end
> end
>
> thanks again for your help

My goodness, I don't know what you want to do but this code hurts my eye.

Bruno

Subject: comparing the elements of two matrix

From: Sean de

Date: 18 Feb, 2011 19:33:04

Message: 9 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> My goodness, I don't know what you want to do but this code hurts my eye.

You mean basic scalar non-expansions ;-)

Subject: comparing the elements of two matrix

From: Bruno Luong

Date: 18 Feb, 2011 19:40:04

Message: 10 of 10

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ijmhhg$4ht$1@fred.mathworks.com>...

>
> You mean basic scalar non-expansions ;-)

Aouch, yes, aouuuch... ;-)

Bruno

Tags for this Thread

No tags are associated with this thread.

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.

Contact us