deleting a column in hansl

67 views Asked by At

I have a very simple question. I want to delete a column from a matrix in a loop.

In Matlab I use the following:

for a certain i,

X(:,i)=[]

which deletes the column an reshapes the matrix.

I want to know the equivalent in Hansl (Gretl) program, please.

Thanks!

1

There are 1 answers

1
Sven S. On BEST ANSWER

Sorry it's probably too late for you now, but I just saw your question and maybe it's useful for others. In hansl (gretl's scripting and matrix language) I could think of several possibilities:

First, if you happen to know the number of columns and the value of i, the solution could use a hard-wired index vector (for i==2 and cols(X)==5 here):

X = X[, {1, 3,4,5}]

Secondly, since the first solution is probably too restrictive, you could concatenate the left and right parts of the matrix, as in:

X = X[, 1: i-1] ~ X[, i+1 :cols(X)]

But the problem here is that i must not index the first or last column, or the indexing will produce an error.

So my final suggestion that should work universally is:

X = selifc( X, ones(1, i-1) ~ 0 ~ ones(1, cols(X) - i) ) 

The selifc() function discards the column for which the second vector argument has a 0 entry. This also works for i==1 or i==cols(X). A shorter variation of this final solution might be:

X = selifc(X, seq(1, cols(X)) .!= i)

which does an element-wise not-equal-to-i comparison (.!=) of the column indices constructed with the seq() function. But it's probably not as readable as the previous way.

good luck!