squeeze

squeeze(A, dims)

Remove the dimensions specified by dims from array A. Elements of dims must be unique and within the range 1:ndims(A).

Examples

  1. Remove singleton dimensions from an array:

    julia> arr = [1, 2, 3];
    julia> arr = reshape(arr, (1, 1, 3));
    julia> squeeze(arr, 1)
    3-element Array{Int64,1}:
    1
    2
    3

    In this example, the squeeze function removes the singleton dimensions (dimension 1) from the array arr, resulting in a 1-dimensional array.

  2. Remove multiple dimensions from a multi-dimensional array:

    julia> arr = [1 2 3; 4 5 6; 7 8 9];
    julia> squeeze(arr, (1, 2))
    3×3 Array{Int64,2}:
    1  2  3
    4  5  6
    7  8  9

    Here, the squeeze function removes dimensions 1 and 2 from the 2-dimensional array arr, resulting in the same array.

  3. Handle out-of-bounds dimensions:
    julia> arr = [1, 2, 3];
    julia> squeeze(arr, 2)
    ERROR: DimensionError: invalid dimension(s) [2] for array of size (3,)

    This example demonstrates that using an out-of-bounds dimension will result in a DimensionError. Ensure that the dimensions specified in dims are within the valid range of the array.

Common mistake example:

julia> arr = [1, 2, 3];
julia> squeeze(arr, (1, 1))
ERROR: ArgumentError: duplicate entries in `dims`

In this example, the dims argument contains duplicate entries, which is not allowed. Each dimension should be specified only once in dims.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: