help-octave
[Top][All Lists]

## Re: Deleting rows in matrices

 From: Qualitang Subject: Re: Deleting rows in matrices Date: Tue, 13 Sep 2011 21:02:17 +0800

```Hi Pascal,

Thanks for the reply! I was getting to something like that when I saw

Thanks again,

Matty.

On Tue, Sep 13, 2011 at 8:49 PM, CdeMills <address@hidden> wrote:
>
> MMolloy wrote:
>>
>> Hi,
>>
>> I am having (another!) problem on a seemingly innocuous task. Say I
>> have many (le x 1) matrices, A, B, C, ......, all of the same length
>> "le". I want to check each of the elements in "A", and if an element
>> exceeds a limit I want to delete that row, plus the corresponding rows
>> in B, C, ...., . Here's my try..
>>
>> le = length(A)
>> for i = 1:le
>> if (A(i,1) > 5)
>> A(i,:) = [];
>> B(i,:) = [];
>> C(i,:) = [];
>> .....
>> endif
>> endfor
>>
>>  But I keep getting errors that say the index exceeds the matrix
>> dimension - I understand why, it's obvious, but I can't think of
>> another method to do this!
>>
>>
>
> The problem is rather simple. You initialise le with the length of A, then
> delete rows. This change A size, but this is not reflected in le. Apart from
> the obvious solution given, you can rewrite your loop as
>
> le = 1; while le <= length(A)
> if (A(le, 1) > 5)
>  A(le, :) = [];
> else
>  le = le + 1;
> endif
>
> This way:
> - if you delete a row, you don't touch le, because it's the row number of
> the NEXT untested line after deletion
> - otherwise, you use standard increment
> In both cases, le is tested against the length of A at each iteration. This
> way, you can't access unexistent lines.
>
> Regards
>
> Pascal
>
> --
> View this message in context:
> http://octave.1599824.n4.nabble.com/Deleting-rows-in-matrices-tp3809655p3809969.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list