```Path: news.mathworks.com!newsfeed-00.mathworks.com!fu-berlin.de!nuzba.szn.dk!pnx.dk!feeder.eternal-september.org!eternal-september.org!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help with a Tricky For Loop
Date: Thu, 08 Oct 2009 12:33:59 -0500
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <hal82q\$pq\$1@news.eternal-september.org>
References: <hal464\$dru\$1@fred.mathworks.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.eternal-september.org U2FsdGVkX19eJeOW7Y+/VnOGLgU18ZyJVGda1G0Bemb9ARp2xGKRcLkDi+Ub1h7TLID8VDOtr/Ma+Qo1R+Tu7VqZLEjBPvfW1EBZ6EZmxo6Hg4KcjU/ZuB23vxK5u2mMWxIVHpxTUGU=
X-Complaints-To: abuse@eternal-september.org
NNTP-Posting-Date: Thu, 8 Oct 2009 17:38:35 +0000 (UTC)
X-Auth-Sender: U2FsdGVkX19Nw3ukeydY/CyIOKOFQxZWHnfWKOVQOAQ=
Cancel-Lock: sha1:8Ag2i5lSY8wPdCtCW28qILpLw3M=
User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
Xref: news.mathworks.com comp.soft-sys.matlab:576013

> I have an array like this a = [2 4; 3 5; 6 7; 6 9].
>
> 1. The program compares consecutive rows of the first column,
> (starting from the first row and second row), checks if the difference
> is equal to 1 or 0.
>
> 2. if (1) is true, then it removes the row with smaller second column.
> 3. If (1) is not true, it goes to the second row , and third row, and compare the two.
>
> In my example above, it compares 2,3(consecutive rows , first
> column), since the difference is 1, it removes first row[2 4] because
> 4 is less than 5( in the second column).
>
> Hence, I now have a new array, [3 5; 6 7;6 9], and I would like it to
> repeat the same: compare first row, second row both of first column. In
> my example, 3,6 difference is 3, so it will leave the rows, then next
> comapre row 2, and row 3 (6,6), since the difference is 0, it will
> delete row [6 7]. because 7 is less than 9 in second column
>
> So my final array will be [3 5; 6 9]

Try this...

>> a = [2 4; 3 5; 6 7; 6 9];
>> d = [zeros(1,size(a,2)); diff(a,1)];
>> b= a(r(:,1)<2 & r(:,2)>0,:)
b =
3     5
6     9
>>

--
```