# Myarray.removeat() For Columns

## Recommended Posts

Is there a syntax to use RemoveAt() to remove column(s) from a 2D array, or possibly another function that can do that?

IOW,

RemoveAtCols({{1,2,3}, {4,5,6}}, 1) would return {{1,3}, {4,6}}.

##### Share on other sites

No there isn't a function, but its easy to do. BTW: removeAt is a member function of a variable, not a global function, so its: myVar.removeAt(4).

Anyhow, its important to understand that you can subset in a variety of ways. One way is to use empty [] meaning you want that entire dimension. So, if you have:

`global x = {{1,2,3},{4,5,6}}[/CODE]you can get just the second column by doing:[CODE]x[][1][/CODE]Alas, concat is first dimension only, however, you can also do assignment this way.  So:[CODE]global y = x[][0]y[][1] = x[][2][/CODE]now y = {{1,3},{4,6}}You can of course put in ranges:[CODE]x[][1,2][/CODE]So, you can actually do this using one variable:[CODE]x[][1] = x[][2,numcols(x)-1]x = x[][0,numcols(x)-2][/CODE]It'd be pretty easy to convert this into a removeAtCols function:[CODE]function removeAtCols(arr, index, nCols)   arr[][index] = arr[][index+nCols, numcols(arr)-1]   arr = arr[][0,numcols(arr)-1-nCols]   return arr[/CODE]The corresponding function for rows (if you didn't want to use the built in removeAt) is the same, but without the [] after each arr, and of course numrows() instead of numcols().  Note that this sort of array manipulation is really quite fast.  Much, much faster than doing any sort of loop in script.`
##### Share on other sites

OK, think I found a way to do this without looping.

`(MyArray == CriteriaCol*) == ColumnOfOnes**[/CODE]`

*in row format

**in column format

Yields a filter()-compatible array of ones and zeros that ID the columns targeted for deletion, which I then could use with filter() or your RemoveAtCols function/method.

Have to check if filter() will work here. The looping way works fine on small files, but if it gets too slow on large files I'll switch to this method.

One caveat is that while I'm removing the columns, I still need to know which columns the remaining data originally occupied, so that may limit my options slightly. What I'm doing in the looping method is creating a sequence array with SeqAdd(0,1,NumCols(MyArray)) then doing .RemoveAt on both the source array and the sequence array, which leaves the sequence array representing the original column source for the retained data.

So what I could do in a non-looping method is assign the above expression to an array, then use that array to filter() both the source array and the sequence array, which would leave the same results I get now but without the looping.