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:
Grouping data

Subject: Grouping data

From: SM

Date: 15 Nov, 2011 23:50:30

Message: 1 of 3

Dear All,
I need help with grouping data stored in matrix. I appreciate your time and your effort in advance.

I have my data stored in mXn matrix. I want to group data in rows and columns. Actually, I already group the data in each row, but I don’t know how to combine the groups in different rows. I try to explain how I want to do the grouping:

For instance data stored in matrix as follow:

[ 0 1 0 0 3 4 0 0 0 0 0 2 3 4 5 0 0 0
  1 0 0 1 1 0 1 0 0 0 1 5 0 0 1 0 0 0
  0 1 2 0 2 1 0 0 0 0 0 0 0 0 0 0 0 2]

1- Go through the row 1 and group the non-zero data in row ignore if there is less than 2 zeros in between.

Basically during the first iteration the result will be:
Gr11= [1 0 0 3 4]
Gr12= [2 3 4 5]

2- Then iterate to the second row and do the same grouping:
Gr21=[ 1 0 0 1 1 0 1]
Gr22=[1 5 0 0 1 ]
In this part I want the algorithm to compare the groups in the current row with the groups in the previous one and in case they overlap or they are in the same vicinity, I want the algorithm to combine the groups and replace them in second row which results in the following:
[ 1 1 0 1 4 4 1 0 0 0 1 7 3 4 6 0 0 0]

Then the groups will be:
GR21_new=[1 1 0 1 4 4 1]
GR22_new=[1 7 3 4 6]

3- The algorithm iterates to next row, perform the same grouping and the result shall be then:
[1 1 2 1 6 5 1 0 0 0 1 7 3 4 6 0 0 2]

GR31_new=[1 1 2 1 6 5 1 ]
GR32_new=[0 1 7 3 4 6 0 0 2]

And so on….
Can you help me with this???
Thanks so much for your help.

Subject: Grouping data

From: Roger Stafford

Date: 16 Nov, 2011 01:26:28

Message: 2 of 3

"SM" wrote in message <j9uts6$o9i$1@newscl01ah.mathworks.com>...
> [ 0 1 0 0 3 4 0 0 0 0 0 2 3 4 5 0 0 0
> 1- Go through the row 1 and group the non-zero data in row ignore if there is less than 2 zeros in between.
>
> Basically during the first iteration the result will be:
> Gr11= [1 0 0 3 4]
> Gr12= [2 3 4 5]
- - - - - - - - - -
  This isn't a complete answer but perhaps I can assist with the "grouping" action of step 1 and leave the other steps to you (or others.)

  Let x be a row in your matrix. Then do this:

 x1 = [x,0,0,0]; x23 = [0,x,0,0]|[0,0,x,0]; x4 = [0,0,0,x];
 f1 = find(x1 & ~(x23|x4));
 f2 = find(x4 & ~(x1|x23))-3;

Then f1 will contain the indices of the start of each "group" in this row and the corresponding f2 values will be the ending indices of these same groups - (f1 and f2 of necessity must possess the same number of indices.) That should suffice to uniquely determine your groups.

  Note that it is quite possible to have different numbers of groups in your various rows, so you will probably have to store them in a cell array.

  Note 2: You stated "ignore if there is less than 2 zeros" but in your example you seemed to "ignore" if there were two or fewer zeros.

Roger Stafford

Subject: Grouping data

From: SM

Date: 16 Nov, 2011 11:43:07

Message: 3 of 3

You are right, indeed I am looking for an algorithm that ignores for instance two zeros or less and group the data...

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