On Thu, 3 Nov 2016, Berend Hasselman wrote:
>> First, it would be an improvement. Second, if at all
possible, I would
>> favor some clever extension of the matrix indexing apparatus. For
>> example (just an example, no claim of optimality), X[-3, -2] might
>> return the matrix X without the 3rd row and the 2nd column.
>
> I appreciate that you said "no claim of optimality", but I don't think
> we should use negative indices for the purpose you indicate. Seems to
> me the de facto standard regarding negative indices in matrix-oriented
> languages is that they mean, "count from the end, not the beginning",
> not "drop these rows or columns".
Try this in R:
A <- matrix(1:12,nrow=3)
A
A[-1,-2]
displays A without first row and second column.
As I expected and hoped.
I normally don't like R very much, but I must admit I find this quite
cool. I guess we could adopt the following convention with respect to
matrix slicing: positive indices mean "keep" and negative indices mean
"drop". My earlier example would just become X[,{-2,-3,-5}], or even
X[,-{2,3,5}].
As for the "from the end" convention, I guess that this is (at least in my
own experience) a rather unusual case, that can be easily handled via
mreverse() or by tricks like
<hansl>
# slice columns 1,3 and 4 from the end
matrix e = (cols(A) + 1) - {1,3,4}
eval A[,e]
</hansl>
The "minus is for dropping" convention would have the advantage of being
(a) intuitive (b) backwards compatible, though we should decide what to do
in the mixed case, eg,
A[,{1,-2}]
Flag an error, perhaps?
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------