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:
Delete inverse rows in the array

Subject: Delete inverse rows in the array

From: Liana

Date: 29 Oct, 2011 03:10:28

Message: 1 of 7

Hi,

I need to delete inverse rows in the array, e.g.
a = [1 2; 2 1; 3 1; 5 1]

2 1 is inverse of 1 2, and therefore it should be deleted. Is there any quick way to do this?

So, it seems that first I should find indices of such rows, and secondly I should delete only inverse ones.

I tried this:
find(a(:,1)==a(:,2) & a(:,2)==a(:,1))
but it does not solve the problem.

Thanks.

Subject: Delete inverse rows in the array

From: Liana

Date: 29 Oct, 2011 03:39:11

Message: 2 of 7

Currently, I found only such solution, but it is bases on 'for loop'. Is there any other more efficient solution? Thanks.

while k <= length(a)
    if ismember(a(k,:),fliplr(a),'rows')
        a(k,:) = [];
    else
        k = k + 1;
    end
end

"Liana" wrote in message <j8fqr4$mds$1@newscl01ah.mathworks.com>...
> Hi,
>
> I need to delete inverse rows in the array, e.g.
> a = [1 2; 2 1; 3 1; 5 1]
>
> 2 1 is inverse of 1 2, and therefore it should be deleted. Is there any quick way to do this?
>
> So, it seems that first I should find indices of such rows, and secondly I should delete only inverse ones.
>
> I tried this:
> find(a(:,1)==a(:,2) & a(:,2)==a(:,1))
> but it does not solve the problem.
>
> Thanks.

Subject: Delete inverse rows in the array

From: Nasser M. Abbasi

Date: 29 Oct, 2011 05:22:46

Message: 3 of 7

On 10/28/2011 10:39 PM, Liana wrote:
> Currently, I found only such solution, but it is bases on 'for loop'. Is there any other
> more efficient solution? Thanks.
>
> while k<= length(a)
> if ismember(a(k,:),fliplr(a),'rows')
> a(k,:) = [];
> else
> k = k + 1;
> end
> end
>

seems like a good solution. I assumed you initialized k to 1
before. A 'for' loop might be simpler than your 'while'.

--Nasser

Subject: Delete inverse rows in the array

From: Nasser M. Abbasi

Date: 29 Oct, 2011 05:26:33

Message: 4 of 7

On 10/29/2011 12:22 AM, Nasser M. Abbasi wrote:
> On 10/28/2011 10:39 PM, Liana wrote:
>> Currently, I found only such solution, but it is bases on 'for loop'. Is there any other
>> more efficient solution? Thanks.
>>
>> while k<= length(a)
>> if ismember(a(k,:),fliplr(a),'rows')
>> a(k,:) = [];
>> else
>> k = k + 1;
>> end
>> end
>>
>
> seems like a good solution. I assumed you initialized k to 1
> before. A 'for' loop might be simpler than your 'while'.
>
> --Nasser
>

I take the comment about the 'for' loop back. I did not
notice the length of the matrix can change during the loop,
so a 'while' loop is the correct one.

--Nasser

Subject: Delete inverse rows in the array

From: Liana

Date: 29 Oct, 2011 05:36:31

Message: 5 of 7

Thanks. I'm using 'while' loop, because the size of 'a' is decreasing each time 'a(k,:) = []' is executed. But this code takes long for large arrays. Therefore, any other solution is highly appreciated.

"Nasser M. Abbasi" <nma@12000.org> wrote in message <j8g2j7$9t3$1@speranza.aioe.org>...
> On 10/28/2011 10:39 PM, Liana wrote:
> > Currently, I found only such solution, but it is bases on 'for loop'. Is there any other
> > more efficient solution? Thanks.
> >
> > while k<= length(a)
> > if ismember(a(k,:),fliplr(a),'rows')
> > a(k,:) = [];
> > else
> > k = k + 1;
> > end
> > end
> >
>
> seems like a good solution. I assumed you initialized k to 1
> before. A 'for' loop might be simpler than your 'while'.
>
> --Nasser
>
>
>

Subject: Delete inverse rows in the array

From: Bruno Luong

Date: 29 Oct, 2011 07:40:16

Message: 6 of 7

 [u I] = unique(sort(a,2),'rows')
 a(I,:)

% Bruno

Subject: Delete inverse rows in the array

From: Liana

Date: 29 Oct, 2011 09:07:29

Message: 7 of 7

Perfect! Thank you. It works very quickly for large scale arrays.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j8gakv$7os$1@newscl01ah.mathworks.com>...
> [u I] = unique(sort(a,2),'rows')
> a(I,:)
>
> % 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