Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a dimension to an array? (opposite of `squeeze`)

Tags:

julia

I can never remember how to do this this.

How can go

  • from a Vector (size (n1)) to a Column Matrix (size (n1,1))?
  • or from a Matrix (size (n1,n2)) to a Array{T,3} (size (n1,n2,1))?
  • or from a Array{T,3} (size (n1,n2,n3)) to a Array{T,4} (size (n1,n2,n3, 1))?
  • and so forth.

I want to know to take Array and use it to define a new Array with an extra singleton trailing dimension. I.e. the opposite of squeeze

like image 889
Lyndon White Avatar asked Feb 18 '17 07:02

Lyndon White


People also ask

How to add a new dimension to a NumPy array?

The numpy.expand_dims () function adds a new dimension to a NumPy array. It takes the array to be expanded and the new axis as arguments. It returns a new array with extra dimensions. We can specify the axis to be expanded in the axis parameter. myArray = np.append (myArray, [ ["R","Go","Kotlin"]], axis=0)

What is the difference between NumPy Squeeze () and NumPy squeeze ()?

squeeze () is also provided as a method of numpy.ndarray. Usage is the same as numpy.squeeze (). The first argument is axis. squeeze () method also returns a view like numpy.squeeze (). The original object remains the same.

How to change the shape of an array in Python?

The initial shape of the array is (4,0). After passing .shape () as (2,2), the shape of the array is changed accordingly. The reshape function takes a parameter order. This allows us to reshape either row or column-wise.

What if dimension is not 1 in AutoCAD?

An error will occur if you specify a dimension whose size is not 1 or a dimension that does not exist. axis can also be specified as a negative value. -1 corresponds to the last dimension and can be specified by the position from the back. You can specify multiple dimensions with tuples.


1 Answers

Try this

function extend_dims(A,which_dim)
       s = [size(A)...]
       insert!(s,which_dim,1)
       return reshape(A, s...)
       end

the variable extend_dim specifies which dimension to extend

Thus

extend_dims(randn(3,3),1)

will produce a 1 x 3 x 3 array and so on.

I find this utility helpful when passing data into convolutional neural networks.

like image 195
Raj Rao Avatar answered Oct 19 '22 04:10

Raj Rao