sgpykit.util.matlab

Functions

ce(*args)

Replacement for the matlab {} operator to create cells.

cell(shape)

Returns an empty cell (numpy) array with the given shape (empty nD-matrix).

eig(var)

Compute the eigenvalues and eigenvectors of a matrix.

fieldnames(s)

Get the field names of a Struct or StructArray.

find(X[, n, direction])

Find non-zero elements in an array.

fliplr(arr)

Flip the elements of the array from left to right.

get_shape_of_cells(objects)

Get the shape of the first Cell object in the list of objects.

ifft(arr)

Compute the inverse FFT of a 1D or 2D array, matching MATLAB's ifft() behavior.

intersect(x, y)

Find the intersection of two arrays.

is_numeric_scalar(item)

Check if an item is a numeric scalar.

iscell(var)

Check if the given variable is a Cell.

isfield(var, sel)

Check if the given field exists in the struct.

isnumeric(obj)

Check if the object is numeric.

issorted(matrix[, typ])

Check if the matrix is sorted.

isstruct(var)

Check if the given variable is an instance of Struct or StructArray.

logical(var)

Convert array to boolean (0=false, else=true).

reshape(var, r[, c])

Reshape an array.

save(filename, *args)

Save NumPy arrays to a MATLAB file.

savemat(file_name, mdict[, appendmat, ...])

Save a dictionary of names and arrays into a MATLAB-style .mat file.

setdiff(A, B[, kind])

Set difference of two arrays.

sort(arr[, axis, direction])

Sort an array in ascending or descending order.

sortrows(A)

Sort rows of a matrix in ascending order.

struct(*args, **kwargs)

Create a Struct or StructArray from given arguments.

unique(var[, by])

Incomplete matlabs unique() function.

Classes

Cell(shape[, dtype])

A 1D or 2D container class that extends Python's built-in list.

Sequence()

All the operations on a read-only sequence.

Struct(**kwargs)

A simple struct-like class that allows attribute access and dictionary-like item access.

StructArray(*args, **kwargs)

sgpykit.util.matlab.ce(*args)[source]

Replacement for the matlab {} operator to create cells. The cell elements must be of same data type.

Parameters:
*argstuple

Elements to be placed in the cell array.

Returns:
Cell

The created cell array.

sgpykit.util.matlab.cell(shape)[source]

Returns an empty cell (numpy) array with the given shape (empty nD-matrix).

2D cell objects can be flat indexed, e.g. c[0,2] or c[2]. Higher dimensions than 2D are not supported. If shape is a single integer N, create a NxN cell

Parameters:
shapeint or tuple

The shape of the cell array. If an integer, creates a square cell array.

Returns:
Cell

The empty cell array.

sgpykit.util.matlab.eig(var)[source]

Compute the eigenvalues and eigenvectors of a matrix.

Parameters:
vararray_like

Input matrix.

Returns:
tuple

Eigenvectors and eigenvalues.

sgpykit.util.matlab.fieldnames(s)[source]

Get the field names of a Struct or StructArray.

Parameters:
sStruct or StructArray

The struct or struct array.

Returns:
list

The list of field names.

sgpykit.util.matlab.find(X, n=None, direction='first')[source]

Find non-zero elements in an array.

Parameters:
Xarray_like

Input array.

nint, optional

Maximum number of elements to return. Default is None.

directionstr, optional

Direction to search (‘first’ or ‘last’). Default is ‘first’.

Returns:
tuple

Indices and values of non-zero elements.

sgpykit.util.matlab.fliplr(arr)[source]

Flip the elements of the array from left to right.

Parameters:
arrarray_like

Input array to be flipped.

Returns:
ndarray

Flipped array.

sgpykit.util.matlab.ifft(arr)[source]

Compute the inverse FFT of a 1D or 2D array, matching MATLAB’s ifft() behavior.

Parameters:
arrarray_like

A 1D or 2D numpy array.

Returns:
ndarray

The inverse FFT of the input array.

Raises:
ValueError

If the input array is not 1D or 2D.

sgpykit.util.matlab.intersect(x, y)[source]

Find the intersection of two arrays.

Parameters:
xarray_like

First input array.

yarray_like

Second input array.

Returns:
tuple

Common elements and their indices in the original arrays.

sgpykit.util.matlab.iscell(var)[source]

Check if the given variable is a Cell.

Parameters:
varany

The variable to check.

Returns:
bool

True if the variable is a Cell, False otherwise.

sgpykit.util.matlab.isfield(var, sel)[source]

Check if the given field exists in the struct.

Parameters:
varstr

The field name to check.

selStruct or StructArray

The struct or struct array.

Returns:
bool

True if the field exists, False otherwise.

sgpykit.util.matlab.isnumeric(obj)[source]

Check if the object is numeric.

Parameters:
objany

The object to check.

Returns:
bool

True if the object is numeric, False otherwise.

sgpykit.util.matlab.issorted(matrix, typ='rows')[source]

Check if the matrix is sorted.

Parameters:
matrixarray_like

Input matrix.

typstr, optional

Type of sorting to check. Default is ‘rows’.

Returns:
bool

True if the matrix is sorted, False otherwise.

sgpykit.util.matlab.isstruct(var)[source]

Check if the given variable is an instance of Struct or StructArray.

Parameters:
varany

The variable to check.

Returns:
bool

True if the variable is an instance of Struct or StructArray, False otherwise.

sgpykit.util.matlab.logical(var)[source]

Convert array to boolean (0=false, else=true).

Parameters:
vararray_like

Input array.

Returns:
ndarray

Boolean array.

sgpykit.util.matlab.reshape(var, r, c=None)[source]

Reshape an array.

Parameters:
vararray_like

Input array.

rint or array_like

New shape or number of rows.

cint, optional

Number of columns. Default is None.

Returns:
ndarray

Reshaped array.

sgpykit.util.matlab.save(filename, *args)[source]

Save NumPy arrays to a MATLAB file.

Parameters:
filenamestr

The name of the file to save.

*argstuple

Variable length argument list of strings and NumPy arrays. The first argument is the filename, followed by pairs of variable names and arrays.

sgpykit.util.matlab.setdiff(A, B, kind='rows')[source]

Set difference of two arrays.

Parameters:
Aarray_like

First input array.

Barray_like

Second input array.

kindstr, optional

Type of set difference (‘rows’ or others). Default is ‘rows’.

Returns:
ndarray

The set difference.

sgpykit.util.matlab.sort(arr, axis=None, direction='ascend')[source]

Sort an array in ascending or descending order.

Parameters:
arrarray_like

Input array.

axisint, optional

Axis along which to sort. Default is None.

directionstr, optional

Direction to sort (‘ascend’ or ‘descend’). Default is ‘ascend’.

Returns:
tuple

Sorted array and sorted indices.

sgpykit.util.matlab.sortrows(A)[source]

Sort rows of a matrix in ascending order.

Parameters:
Aarray_like

Input array.

Returns:
tuple

Sorted array and sorted indices.

sgpykit.util.matlab.struct(*args, **kwargs)[source]

Create a Struct or StructArray from given arguments.

This function mimics MATLAB’s struct behavior. If keyword arguments are provided, it creates a Struct with the given fields. If positional arguments are provided, it creates a Struct with fields from the even-indexed arguments and values from the odd-indexed arguments. If cells are detected, it creates a StructArray.

Parameters:
*argstuple

Positional arguments for field-value pairs.

**kwargsdict

Keyword arguments for field-value pairs.

Returns:
Struct or StructArray

The created struct or struct array.

sgpykit.util.matlab.unique(var, by=None)[source]

Incomplete matlabs unique() function.

https://de.mathworks.com/help/matlab/ref/double.unique.html - ‘first’ and ‘sorted’ are defaults https://numpy.org/doc/stable/reference/generated/numpy.unique.html#numpy-unique

Parameters:
vararray_like

Input array.

bystr, optional

The axis to return indices for repetitions. Default is None.

Returns:
tuple

The unique elements and their indices.