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:
Matching rows in one matrix to elements of another matrix

Subject: Matching rows in one matrix to elements of another matrix

From: jrockv6e

Date: 17 May, 2011 14:11:04

Message: 1 of 8

Hi, I'm having trouble with this part of my code. I'll try to simplify the problem to explain.

I have the following matrices:
A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2];
B= [1; 2; 3; 4];

And I know the following Rules:
If row = 1 1 1 then output = 1
If row = 1 1 2 then output = 2
And so on…

So I’m looking to assign a number in B to a row in A.
So my output would look like:
C = [ 1; 1; 2; 2; 2; 3; 4]

I’m having trouble since 1 1 1 is a row, I tried combing the row into a single number 111 but I’m having trouble making it work. I hope I was clear.

Thanks for the help!

Subject: Matching rows in one matrix to elements of another matrix

From: Wayne King

Date: 17 May, 2011 14:31:02

Message: 2 of 8

"jrockv6e" wrote in message <iqtvln$9mp$1@newscl01ah.mathworks.com>...
> Hi, I'm having trouble with this part of my code. I'll try to simplify the problem to explain.
>
> I have the following matrices:
> A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2];
> B= [1; 2; 3; 4];
>
> And I know the following Rules:
> If row = 1 1 1 then output = 1
> If row = 1 1 2 then output = 2
> And so on…
>
> So I’m looking to assign a number in B to a row in A.
> So my output would look like:
> C = [ 1; 1; 2; 2; 2; 3; 4]
>
> I’m having trouble since 1 1 1 is a row, I tried combing the row into a single number 111 but I’m having trouble making it work. I hope I was clear.
>
> Thanks for the help!

A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2];
[B,I,J] = unique(A,'rows');
J % This is your C vector above

Wayne

Subject: Matching rows in one matrix to elements of another matrix

From: jrockv6e

Date: 17 May, 2011 15:24:03

Message: 3 of 8

"Wayne King" <wmkingty@gmail.com> wrote in message <iqu0r6$ds9$1@newscl01ah.mathworks.com>...
> "jrockv6e" wrote in message <iqtvln$9mp$1@newscl01ah.mathworks.com>...
> > Hi, I'm having trouble with this part of my code. I'll try to simplify the problem to explain.
> >
> > I have the following matrices:
> > A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2];
> > B= [1; 2; 3; 4];
> >
> > And I know the following Rules:
> > If row = 1 1 1 then output = 1
> > If row = 1 1 2 then output = 2
> > And so on…
> >
> > So I’m looking to assign a number in B to a row in A.
> > So my output would look like:
> > C = [ 1; 1; 2; 2; 2; 3; 4]
> >
> > I’m having trouble since 1 1 1 is a row, I tried combing the row into a single number 111 but I’m having trouble making it work. I hope I was clear.
> >
> > Thanks for the help!
>
> A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2];
> [B,I,J] = unique(A,'rows');
> J % This is your C vector above
>
> Wayne

Thanks for the response Wayne. But I think I simplified the problem too much. Your code works for vector B= [1; 2; 3; 4]; But my B would not contain integers. For instance let B vector be:

B=[0.25; 0.35; 0.45; 0.55];

so my C vector would be:

C = [ 0.25; 0.25; 0.35; 0.35; 0.35; 0.45; 0.55]

Thanks

Subject: Matching rows in one matrix to elements of another matrix

From: Roger Stafford

Date: 17 May, 2011 16:26:03

Message: 4 of 8

"jrockv6e" wrote in message <iqu3uj$ort$1@newscl01ah.mathworks.com>...
> "Wayne King" <wmkingty@gmail.com> wrote in message <iqu0r6$ds9$1@newscl01ah.mathworks.com>...
> > A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2];
> > [B,I,J] = unique(A,'rows');
> > J % This is your C vector above
> > Wayne
>
> Thanks for the response Wayne. But I think I simplified the problem too much. Your code works for vector B= [1; 2; 3; 4]; But my B would not contain integers. For instance let B vector be:
>
> B=[0.25; 0.35; 0.45; 0.55];
>
> so my C vector would be:
>
> C = [ 0.25; 0.25; 0.35; 0.35; 0.35; 0.45; 0.55]
>
> Thanks
- - - - - - - -
  Just use Wayne's 'J' as indices:

 C = B(J);

(where B is your original B, not Wayne's B.) Of course this depends on B having the same number of elements as distinct rows in A, (or at least that number.)

Roger Stafford

Subject: Matching rows in one matrix to elements of another matrix

From: jrockv6e

Date: 17 May, 2011 16:45:07

Message: 5 of 8

"Roger Stafford" wrote in message <iqu7ir$8a7$1@newscl01ah.mathworks.com>...
> "jrockv6e" wrote in message <iqu3uj$ort$1@newscl01ah.mathworks.com>...
> > "Wayne King" <wmkingty@gmail.com> wrote in message <iqu0r6$ds9$1@newscl01ah.mathworks.com>...
> > > A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2];
> > > [B,I,J] = unique(A,'rows');
> > > J % This is your C vector above
> > > Wayne
> >
> > Thanks for the response Wayne. But I think I simplified the problem too much. Your code works for vector B= [1; 2; 3; 4]; But my B would not contain integers. For instance let B vector be:
> >
> > B=[0.25; 0.35; 0.45; 0.55];
> >
> > so my C vector would be:
> >
> > C = [ 0.25; 0.25; 0.35; 0.35; 0.35; 0.45; 0.55]
> >
> > Thanks
> - - - - - - - -
> Just use Wayne's 'J' as indices:
>
> C = B(J);
>
> (where B is your original B, not Wayne's B.) Of course this depends on B having the same number of elements as distinct rows in A, (or at least that number.)
>
> Roger Stafford


Thanks Roger!

Subject: Matching rows in one matrix to elements of another matrix

From: jrockv6e

Date: 17 May, 2011 18:58:02

Message: 6 of 8

"Roger Stafford" wrote in message <iqu7ir$8a7$1@newscl01ah.mathworks.com>...
> "jrockv6e" wrote in message <iqu3uj$ort$1@newscl01ah.mathworks.com>...
> > "Wayne King" <wmkingty@gmail.com> wrote in message <iqu0r6$ds9$1@newscl01ah.mathworks.com>...
> > > A = [ 1 1 1; 1 1 1; 1 1 2; 1 1 2; 1 1 2; 1 2 1; 1 2 2];
> > > [B,I,J] = unique(A,'rows');
> > > J % This is your C vector above
> > > Wayne
> >
> > Thanks for the response Wayne. But I think I simplified the problem too much. Your code works for vector B= [1; 2; 3; 4]; But my B would not contain integers. For instance let B vector be:
> >
> > B=[0.25; 0.35; 0.45; 0.55];
> >
> > so my C vector would be:
> >
> > C = [ 0.25; 0.25; 0.35; 0.35; 0.35; 0.45; 0.55]
> >
> > Thanks
> - - - - - - - -
> Just use Wayne's 'J' as indices:
>
> C = B(J);
>
> (where B is your original B, not Wayne's B.) Of course this depends on B having the same number of elements as distinct rows in A, (or at least that number.)
>
> Roger Stafford


Sorry to bring this topic back, but it seems that I’m still having problems, in part due to my explanation and trying to simplify the problem. I will try to explain in much clearer way. I have the following:

- matrix b with a set number of values (5 in this case)
b = [0.0276; 0.0129; 0.4283; 0.4621; 0.9996];

- matrix f has a corresponding row for each value of b
f = [1 1 1; 1 1 2; 1 2 1; 2 1 1; 2 2 1];

as shown in this image: http://i.imgur.com/U5AcB.png


So my code generates matrix j which can have infinite amount of rows in the same format as in matrix f. I want to assign each row in 'j' a corresponding b value.

as shown in this image: http://i.imgur.com/2fyN0.png

so for this example if my is: j = [ 1 1 1; 1 1 1; 1 2 1; 1 2 1; 2 2 2];

my output would be: x=[0.0276; 0.0276; 0.4283; 0.4283; 0.996];

I hope I was clear. Thanks.

Subject: Matching rows in one matrix to elements of another matrix

From: Roger Stafford

Date: 17 May, 2011 20:14:04

Message: 7 of 8

"jrockv6e" wrote in message <iqugfq$amg$1@newscl01ah.mathworks.com>...
> Sorry to bring this topic back, but it seems that I’m still having problems, in part due to my explanation and trying to simplify the problem. I will try to explain in much clearer way. I have the following:
>
> - matrix b with a set number of values (5 in this case)
> b = [0.0276; 0.0129; 0.4283; 0.4621; 0.9996];
>
> - matrix f has a corresponding row for each value of b
> f = [1 1 1; 1 1 2; 1 2 1; 2 1 1; 2 2 1];
>
> as shown in this image: http://i.imgur.com/U5AcB.png
>
> So my code generates matrix j which can have infinite amount of rows in the same format as in matrix f. I want to assign each row in 'j' a corresponding b value.
>
> as shown in this image: http://i.imgur.com/2fyN0.png
>
> so for this example if my is: j = [ 1 1 1; 1 1 1; 1 2 1; 1 2 1; 2 2 2];
>
> my output would be: x=[0.0276; 0.0276; 0.4283; 0.4283; 0.996];
>
> I hope I was clear. Thanks.
- - - - - - - - - -
  What you have just described is very different from the two-array problem previously presented.

  For this problem you need the 'ismember' function using the 'rows' option. I challenge you to figure out how to use the 'loc' (the second) output of 'ismember' to accomplish the task you are asking about. You can do it in just two easy lines.

Roger Stafford

Subject: Matching rows in one matrix to elements of another matrix

From: jrockv6e

Date: 17 May, 2011 20:52:03

Message: 8 of 8

"Roger Stafford" wrote in message <iqukuc$q9n$1@newscl01ah.mathworks.com>...
> "jrockv6e" wrote in message <iqugfq$amg$1@newscl01ah.mathworks.com>...
> > Sorry to bring this topic back, but it seems that I’m still having problems, in part due to my explanation and trying to simplify the problem. I will try to explain in much clearer way. I have the following:
> >
> > - matrix b with a set number of values (5 in this case)
> > b = [0.0276; 0.0129; 0.4283; 0.4621; 0.9996];
> >
> > - matrix f has a corresponding row for each value of b
> > f = [1 1 1; 1 1 2; 1 2 1; 2 1 1; 2 2 1];
> >
> > as shown in this image: http://i.imgur.com/U5AcB.png
> >
> > So my code generates matrix j which can have infinite amount of rows in the same format as in matrix f. I want to assign each row in 'j' a corresponding b value.
> >
> > as shown in this image: http://i.imgur.com/2fyN0.png
> >
> > so for this example if my is: j = [ 1 1 1; 1 1 1; 1 2 1; 1 2 1; 2 2 2];
> >
> > my output would be: x=[0.0276; 0.0276; 0.4283; 0.4283; 0.996];
> >
> > I hope I was clear. Thanks.
> - - - - - - - - - -
> What you have just described is very different from the two-array problem previously presented.
>
> For this problem you need the 'ismember' function using the 'rows' option. I challenge you to figure out how to use the 'loc' (the second) output of 'ismember' to accomplish the task you are asking about. You can do it in just two easy lines.
>
> Roger Stafford

Thanks for the hints Roger. I got it to work.

[output, index] = ismember(j, f, 'rows')
x = b(index)

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