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:
Complex Matrix Row Deletion

Subject: Complex Matrix Row Deletion

From: curoch

Date: 17 Jul, 2011 18:00:10

Message: 1 of 10

Hi all,

My problem is as follows:

- I have a large matrix which may be exemplified in the following manner

A = [1 1 23
       1 1 24
       1 -1 34
       1 -1 45
       2 1 77
       2 1 12
       2 -1 4 ]

- My aim is to analyse the elements in the third column for each set where the values in column 1 and 2 are equal.
- Upon analysing, I need to delete the row which has the smallest value in column 3.

For example, applying this to matrix A

The first set is [ 1 1 23
                      1 1 24] hence deleting the first row

Matrix A thus becomes

A = [ 1 1 23
        1 -1 34
        2 1 12
        2 -1 4]

Thanks a lot for any help,

Regards,

F

Subject: Complex Matrix Row Deletion

From: dpb

Date: 17 Jul, 2011 19:17:27

Message: 2 of 10

On 7/17/2011 1:00 PM, curoch wrote:
> Hi all,
>
> My problem is as follows:
>
> - I have a large matrix which may be exemplified in the following
> manner
>
> A = [1 1 23
> 1 1 24
...
> 2 -1 4 ]
> - My aim is to analyse the elements in the third column for each set
> where the values in column 1 and 2 are equal.

i
> - Upon analysing, I need to delete the row which has the smallest value
> in column 3.
>
> For example, applying this to matrix A
>
> The first set is [ 1 1 23
> 1 1 24] hence deleting the first row

 >> idx=A(:,1)==A(:,2);
 >> z=A(idx,3)

z =

     23
     24

 >> A(idx(1),:)=[]
A =
      1 1 24
      1 -1 34
      1 -1 45
      2 1 77
      2 1 12
      2 -1 4

 >>
>
> Matrix A thus becomes
>
> A = [ 1 1 23
> 1 -1 34
> 2 1 12
> 2 -1 4]
>
...

I don't see the above from the description and the matrix given but
perhaps the previous manipulations will be of some benefit in selecting
and deselecting desired rows, etc., ...

--

Subject: Complex Matrix Row Deletion

From: Roger Stafford

Date: 18 Jul, 2011 00:11:08

Message: 3 of 10

"curoch" wrote in message <ivv7va$kan$1@newscl01ah.mathworks.com>...
> ........
> A = [1 1 23
> 1 1 24
> 1 -1 34
> 1 -1 45
> 2 1 77
> 2 1 12
> 2 -1 4 ]
>
> - My aim is to analyse the elements in the third column for each set where the values in column 1 and 2 are equal.
> - Upon analysing, I need to delete the row which has the smallest value in column 3.
>
> For example, applying this to matrix A
>
> The first set is [ 1 1 23
> 1 1 24] hence deleting the first row
>
> Matrix A thus becomes
>
> A = [ 1 1 23
> 1 -1 34
> 2 1 12
> 2 -1 4]
> ......
- - - - - - - - - -
  Perhaps you should clarify your question, Curoch. You stated "I need to delete the row which has the smallest value in column 3" but then you proceeded to delete the larger one instead. Also you haven't said what is to happen if three or more rows have column 1 all alike and column 2 all alike. How many are to be deleted and which one(s)? In the case of the single row with 2 and -1 you didn't delete anything at all, so what is the proper generalization?

Roger Stafford

Subject: Complex Matrix Row Deletion

From: curoch

Date: 18 Jul, 2011 15:52:09

Message: 4 of 10

"Roger Stafford" wrote in message <ivvtms$dil$1@newscl01ah.mathworks.com>...
> "curoch" wrote in message <ivv7va$kan$1@newscl01ah.mathworks.com>...
> > ........
> > A = [1 1 23
> > 1 1 24
> > 1 -1 34
> > 1 -1 45
> > 2 1 77
> > 2 1 12
> > 2 -1 4 ]
> >
> > - My aim is to analyse the elements in the third column for each set where the values in column 1 and 2 are equal.
> > - Upon analysing, I need to delete the row which has the smallest value in column 3.
> >
> > For example, applying this to matrix A
> >
> > The first set is [ 1 1 23
> > 1 1 24] hence deleting the first row
> >
> > Matrix A thus becomes
> >
> > A = [ 1 1 23
> > 1 -1 34
> > 2 1 12
> > 2 -1 4]
> > ......
> - - - - - - - - - -
> Perhaps you should clarify your question, Curoch. You stated "I need to delete the row which has the smallest value in column 3" but then you proceeded to delete the larger one instead. Also you haven't said what is to happen if three or more rows have column 1 all alike and column 2 all alike. How many are to be deleted and which one(s)? In the case of the single row with 2 and -1 you didn't delete anything at all, so what is the proper generalization?
>
> Roger Stafford

Dear Roger,

Thank you for replying. It appears as you have stated that my post contains some inaccuracies which obviously makes it harder for you guys to help me. I hope this post will make everything clear.

1. I should have said "Delete the row with the largest element in Column 3" thus deleting the 2nd row with element 24 in column 3. My apologies. Unfortunately, I cannot edit my previous post so please ignore it.

2. In terms of proper generalisation, the best way I can explain is by partitioning the matrix. A new example

B = [ 1 1 123
        1 1 434
        1 1 111
        1 -1 434
        1 -1 666
        2 1 12
        2 -1 44
        2 -1 33 ]

I need to analyse each set within matrix B where the element in column 1 is equal. So the first set is as follows

[ 1 1 123, 1 1 434, 1 1 111, 1 -1 434, 1 -1 666]

Within each set, I need to first of all analyse the rows where the elements in column 1 and 2 are equal. Therefore [1 1 123, 1 1 434, 1 1 111].

From there, I need to keep the row with the smallest element in column 3.

I then need to analyse the remaining rows within set 1 which is [1 -1 434, 1 -1 666], also keeping the row with the smallest element in column 3.

These "sets" or "partitions" vary in length and the same may be said about the inner sets. Some sets have a length of 5, others 2 etc...

3. In the case of a single row (where column 1 = column 2 and column 1 ~= column 2), nothing is performed on the row.

4. To add, for any set of rows, the element in column 3 will never be equal so there will never be an instance when there is no minimum.

Upon successful completion, matrix B will take the following appearance

B = [ 1 1 111
        1 -1 434
        2 1 12
        2 -1 33]

5. Just to clarify, the elements in column 1 and 2 are more complex than 1 and -1, the matrix could be in this form for instance

C = [ 2.321 2.321 324
        2.321 2.321 121
        2.321 0.432 111
        2.321 0.432 114 ]

6. As a last point, in each set, there are only one possibility when column 1 does not equal column 2.

For instance, the matrix can never take the following form

D = [ 12 0.5 1
        12 0.5 2
        12 1 3
        12 10 4]

I hope this clears everything up,

Do not hesitate to let me know if something needs clarifying,

Regards,

CF

Subject: Complex Matrix Row Deletion

From: Roger Stafford

Date: 18 Jul, 2011 16:37:08

Message: 5 of 10

"curoch" wrote in message <j01kr9$93u$1@newscl01ah.mathworks.com>...
> ......
> B = [ 1 1 123
> 1 1 434
> 1 1 111
> 1 -1 434
> 1 -1 666
> 2 1 12
> 2 -1 44
> 2 -1 33 ]
> ......
> Upon successful completion, matrix B will take the following appearance
>
> B = [ 1 1 111
> 1 -1 434
> 2 1 12
> 2 -1 33]
> .........
> 6. As a last point, in each set, there are only one possibility when column 1 does not equal column 2.
>
> For instance, the matrix can never take the following form
>
> D = [ 12 0.5 1
> 12 0.5 2
> 12 1 3
> 12 10 4]
> ........
- - - - - - - - - -
 A = sortrows(A);
 [~,m] = unique(A(:,1:2),'rows','first');
 A = A(m,:);

Notes:

1. The first line with 'sortrows' will alter the order of the rows if they are not already in lexicographic order.

2. If your elements are of fractional form, such as the 2.321 of your example, the comparison of elements done by 'unique' nevertheless requires exact equality. Two numbers can display as 2.321 and yet not be exactly equal, perhaps differing only in the least few bits.

3. I could not make sense of your point 6. I don't understand what there is about the form of example D that your matrix can never take.

Roger Stafford

Subject: Complex Matrix Row Deletion

From: dpb

Date: 18 Jul, 2011 17:50:56

Message: 6 of 10

On 7/18/2011 11:37 AM, Roger Stafford wrote:
...

> 3. I could not make sense of your point 6. I don't understand what there
> is about the form of example D that your matrix can never take.
...

I think he's saying that the generating process for the original array
is such that if A(:,1)~=A(:,2) for some (set of) (:) that there is only
one occurrence of that particular combination--iow, (:)-->n where n is a
single value, not a vector. This would only seem to be significant in
that in that case there isn't a row to be thrown away...

--

Subject: Complex Matrix Row Deletion

From: Bruno Luong

Date: 19 Jul, 2011 06:22:07

Message: 7 of 10

Try this:

B = [ 1 1 123
    1 1 434
    1 1 111
    1 -1 434
    1 -1 666
    2 1 12
    2 -1 44
    2 -1 33 ]

[S I J] = unique(B(:,1:2),'rows');
B = [S accumarray(J,B(:,3),[],@min)]

% Bruno

Subject: Complex Matrix Row Deletion

From: Bruno Luong

Date: 19 Jul, 2011 07:08:08

Message: 8 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j037qf$pna$1@newscl01ah.mathworks.com>...

> [S I J] = unique(B(:,1:2),'rows');
> B = [S accumarray(J,B(:,3),[],@min)]
>

If you prefer to order B same as the original, add these two commands :

[trash idx]=sort(I);
B = B(idx,:);

 % Bruno

Subject: Complex Matrix Row Deletion

From: curoch

Date: 19 Jul, 2011 21:48:09

Message: 9 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j03agn$2lt$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j037qf$pna$1@newscl01ah.mathworks.com>...
>
> > [S I J] = unique(B(:,1:2),'rows');
> > B = [S accumarray(J,B(:,3),[],@min)]
> >
>
> If you prefer to order B same as the original, add these two commands :
>
> [trash idx]=sort(I);
> B = B(idx,:);
>
> % Bruno

Dear Bruno,

Thank you so much. To extend your answer

If matrix B were of size (m,6) where column 3,5,6 in the new matrix would correspond to column 1,2,3 in the old example, how would I proceed so that I could keep all columns in the new matrix rather than deleting columns 1,2 and 4?

@ Roger Stafford

Thank you very much for your comments and assistance

@ dpb

Your comment is exactly what I was trying to get at.

Subject: Complex Matrix Row Deletion

From: Bruno Luong

Date: 20 Jul, 2011 07:20:09

Message: 10 of 10

"curoch" wrote in message <j04u2p$1f4$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>
> Thank you so much. To extend your answer
>
> If matrix B were of size (m,6) where column 3,5,6 in the new matrix would correspond to column 1,2,3 in the old example, how would I proceed so that I could keep all columns in the new matrix rather than deleting columns 1,2 and 4?
>

function imin = findminloc(loc, B3)
[trash, imin] = min(B3(loc));
imin = loc(imin);
end % findminloc, save it in another file

%% test script
% Data
B = rand(8,6);
B(:,4:6) = [ 1 1 123
    1 1 434
    1 1 111
    1 -1 434
    1 -1 666
    2 1 12
    2 -1 44
    2 -1 33 ]

% Engine
[S I J] = unique(B(:,4:5),'rows');
m = size(B,1);
I = accumarray(J,(1:m)',[], @(loc) findminloc(loc, B(:,6)));
B = B(sort(I),:)

% Bruno

Tags for 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