[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: colon equivalence when deleting elements
From: |
pkienzle |
Subject: |
Re: colon equivalence when deleting elements |
Date: |
Sat, 18 Oct 2003 00:34:38 +0100 |
On 17 Oct 2003 at 9:03, John W. Eaton wrote:
> On 17-Oct-2003, address@hidden <address@hidden> wrote:
>
> | The a(:,:) = [] case is ambiguous and IMHO should be
> | disallowed.
>
> I don't see why it is ambiguous. The indices name all rows and all
> columns, so it seems that it should delete everything and return a 0x0
> empty matrix. What are the other possibilities?
The other possibilities are that you are deleting all rows or you
are deleting all columns. Since there is no indication of which
you are deleting, you could assume that you are deleting both,
and return [](0x0), but this is needlessly incompatible. You
could adopt compatibility and assume that a(:,:)=[] is the same
as a(1:end,:) and return [](0xN), but that doesn't seem like the
right thing to do That's why I prefer to reject the construct
entirely.
Also, what are you going to do with a(:,i,j)=[]? Removing
slices makes the most sense if only one index is specified,
and the rest use colon, so a(:,:,:)=[] would be a special case.
>
> | > Also, the following
> | >
> | > a = 1; a(1,1) = []
> | >
> | > is not allowed ("Indexed empty matrix assignment is not allowed).
> | > Apparently at least one of the indices must be a colon.
> |
> | For an N-d array, N-1 of the dimensions must be a colon
> | so that the entire slice is removed. If not then you would get
> | surprising results, such as a(i,j) = [] deleting all of row i and
> | all of column j.
>
> OK, I see that, but what I am asking is why it should matter if the
> index is a colon or scalar or a range or a matrix (elements in any
> order) as long as they name all the rows, columns, pages, etc. and are
> therefore equivalent to a colon.
Consider what happens if you are deleting entire rows:
a(idx,:) = [];
Then the resulting shape will be M-k by N, where k is the
number of unique elements in idx. Do you want different
behaviour for M-k==0? Similarly, when deleting columns,
the resulting shape will be M by N-k. Do you want different
behaviour for N-k==0?
What about
a(1:end,1:end) = [];
Are you deleting all rows or all columns?
You can disambiguate using one of the following:
a(:,1:end) = [];
a(1:end,:) = [];
Assuming you reject a(i,j)=[] for i,j not 1:end, you can only
write a consistent story if you also reject a(i,1:end)=[].
At the moment, I don't care about these corner cases. In
the future, I may want consistency or compatibility depending
on what I'm doing :-(
Paul Kienzle
address@hidden