Function Repository Resource:

DropColumn

Source Notebook

Drop columns from matrices

Contributed by: Michael Sollami

ResourceFunction["DropColumn"][mat,n]

gives mat with its first n columns dropped.

ResourceFunction["DropColumn"][mat,-n]

gives mat with its last n columns dropped.

ResourceFunction["DropColumn"][mat,{n}]

gives mat with its nth column dropped.

ResourceFunction["DropColumn"][mat,{m,n}]

gives mat with columns m through n dropped.

ResourceFunction["DropColumn"][mat,{m,n,s}]

gives mat with columns m through n in steps of s dropped.

ResourceFunction["DropColumn"][spec]

represents an operator form of ResourceFunction["DropColumn"] that can be applied to an expression.

Details and Options

ResourceFunction["DropColumn"] works with Span as well as the standard sequence specification.
ResourceFunction["DropColumn"] can only be used on tensors of rank 2 or more, e.g. matrices.
ResourceFunction["DropColumn"] does not work with SparseArray objects or with Association.

Examples

Basic Examples (1) 

Drop the first two columns from a matrix:

In[1]:=
ResourceFunction["DropColumn"][{{a, b, c}, {d, e, f}}, 2] // MatrixForm
Out[1]=

Scope (5) 

Drop the third column from a matrix:

In[2]:=
ResourceFunction["DropColumn"][{{a, b, c, d}}, {3}] // MatrixForm
Out[2]=

Drop the odd columns from a matrix:

In[3]:=
ResourceFunction["DropColumn"][
  Array[Subscript[a, #1, #2] &, {4, 10}], ;; ;; 2] // MatrixForm
Out[3]=

Drop the last eight columns from a matrix:

In[4]:=
ResourceFunction["DropColumn"][
  RandomReal[1, {4, 10}], -8] // MatrixForm
Out[4]=

Drop the third column from a tensor:

In[5]:=
t = Array[Subscript[a, #1, #2, #3] &, {2, 4, 2}]; t // MatrixForm
Out[5]=
In[6]:=
ResourceFunction["DropColumn"][t, {3}] // MatrixForm
Out[6]=

Use the operator form of DropColumn:

In[7]:=
ResourceFunction["DropColumn"][1 ;; 6 ;; 2]@{Range[10]}
Out[7]=

Publisher

Michael Sollami

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 08 March 2019

License Information