Got Questions? Get Answers.
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:
Combining elements in rows into single numbers and finding corresponding elements

Subject: Combining elements in rows into single numbers and finding corresponding elements

From: jrockv6e

Date: 16 May, 2011 23:53:04

Message: 1 of 5

This is a picture of a simple example of what I want to do.

http://i.imgur.com/eZlED.png

Where Y, F, and W are matrices.


My initial idea is to combine all the elements in each row of F into a single number. I have looked at concatenating but it doesn't seem to do what I want.

So that would give me,
e = [111; 111; 222; 222]

and then I need to find the corresponding values of Y and W that fall within the group 111 to do the operation.


I'm heading in the right direction? any hints would be greatly appreciated. Thanks for looking!

Subject: Combining elements in rows into single numbers and finding corresponding elements

From: Roger Stafford

Date: 17 May, 2011 00:43:03

Message: 2 of 5

"jrockv6e" wrote in message <iqsdcv$b41$1@newscl01ah.mathworks.com>...
> This is a picture of a simple example of what I want to do.
>
> http://i.imgur.com/eZlED.png
>
> Where Y, F, and W are matrices.
>
> My initial idea is to combine all the elements in each row of F into a single number. I have looked at concatenating but it doesn't seem to do what I want.
>
> So that would give me,
> e = [111; 111; 222; 222]
>
> and then I need to find the corresponding values of Y and W that fall within the group 111 to do the operation.
>
> I'm heading in the right direction? any hints would be greatly appreciated. Thanks for looking!
- - - - - - - - - -
  I have trouble understanding exactly what you want. The following seems the most likely solution, but if that is not correct, you need to expand considerably upon what you wrote. In particular your array 'e' does not seem compatible with the referenced file, "eZlED.png".

 [u,~,n] = unique(F,'rows');
 C = accumarray(n,Y,[length(u),1]) ./ accumarray(n,W,[length(u),1]);

Roger Stafford

Subject: Combining elements in rows into single numbers and finding corresponding elements

From: jrockv6e

Date: 17 May, 2011 01:02:03

Message: 3 of 5

"Roger Stafford" wrote in message <iqsgan$j9u$1@newscl01ah.mathworks.com>...
> "jrockv6e" wrote in message <iqsdcv$b41$1@newscl01ah.mathworks.com>...
> > This is a picture of a simple example of what I want to do.
> >
> > http://i.imgur.com/eZlED.png
> >
> > Where Y, F, and W are matrices.
> >
> > My initial idea is to combine all the elements in each row of F into a single number. I have looked at concatenating but it doesn't seem to do what I want.
> >
> > So that would give me,
> > e = [111; 111; 222; 222]
> >
> > and then I need to find the corresponding values of Y and W that fall within the group 111 to do the operation.
> >
> > I'm heading in the right direction? any hints would be greatly appreciated. Thanks for looking!
> - - - - - - - - - -
> I have trouble understanding exactly what you want. The following seems the most likely solution, but if that is not correct, you need to expand considerably upon what you wrote. In particular your array 'e' does not seem compatible with the referenced file, "eZlED.png".
>
> [u,~,n] = unique(F,'rows');
> C = accumarray(n,Y,[length(u),1]) ./ accumarray(n,W,[length(u),1]);
>
> Roger Stafford

Thanks Roger that does the job. I guess there was no need for 'e'.

Is there a reason why the output has a 'NaN' ? This is what I have.

Y=[1; 2; 3; 4; 5];
F = [1 1 1; 1 1 1; 1 1 1; 2 2 2; 2 2 2];
W= [9; 8; 7; 6; 5];
[u,z,n] = unique(F,'rows');
C = accumarray(n,Y,[length(u),1]) ./ accumarray(n,W,[length(u),1])

C =
    0.2500
    0.8182
       NaN

Subject: Combining elements in rows into single numbers and finding corresponding elements

From: Roger Stafford

Date: 17 May, 2011 01:26:03

Message: 4 of 5

"jrockv6e" wrote in message <iqsheb$mdq$1@newscl01ah.mathworks.com>...
> .........
> Is there a reason why the output has a 'NaN' ? This is what I have.
>
> Y=[1; 2; 3; 4; 5];
> F = [1 1 1; 1 1 1; 1 1 1; 2 2 2; 2 2 2];
> W= [9; 8; 7; 6; 5];
> [u,z,n] = unique(F,'rows');
> C = accumarray(n,Y,[length(u),1]) ./ accumarray(n,W,[length(u),1])
>
> C =
> 0.2500
> 0.8182
> NaN
- - - - - - - - -
  That's an error on my part. I overlooked the fact that u would have three columns, whereas I needed just its row count to hand to 'accumarray'. I should have written:

 [u,~,n] = unique(F,'rows');
 C = accumarray(n,Y,[size(u,1),1]) ./ accumarray(n,W,[size(u,1),1]);

Roger Stafford

Subject: Combining elements in rows into single numbers and finding corresponding elements

From: jrockv6e

Date: 17 May, 2011 01:42:02

Message: 5 of 5

"Roger Stafford" wrote in message <iqsirb$q19$1@newscl01ah.mathworks.com>...
> "jrockv6e" wrote in message <iqsheb$mdq$1@newscl01ah.mathworks.com>...
> > .........
> > Is there a reason why the output has a 'NaN' ? This is what I have.
> >
> > Y=[1; 2; 3; 4; 5];
> > F = [1 1 1; 1 1 1; 1 1 1; 2 2 2; 2 2 2];
> > W= [9; 8; 7; 6; 5];
> > [u,z,n] = unique(F,'rows');
> > C = accumarray(n,Y,[length(u),1]) ./ accumarray(n,W,[length(u),1])
> >
> > C =
> > 0.2500
> > 0.8182
> > NaN
> - - - - - - - - -
> That's an error on my part. I overlooked the fact that u would have three columns, whereas I needed just its row count to hand to 'accumarray'. I should have written:
>
> [u,~,n] = unique(F,'rows');
> C = accumarray(n,Y,[size(u,1),1]) ./ accumarray(n,W,[size(u,1),1]);
>
> Roger Stafford

Thanks!

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