Skip to main content

A small utility to convert MATLAB-style matrix strings to Python/torch syntax

Project description

MATLAB-to-Python Matrix Converter

This library provides a set of utility functions to convert MATLAB-style matrix expressions into valid Python/NumPy/Torch formats. It handles common indexing, matrix formatting, and math function conversion tasks, allowing MATLAB snippets to be reused directly in Python with minimal manual adjustment.


Features

  • Index Conversion: Converts MATLAB's x(i) (1-based) indexing to Python's x[i-1] (0-based).
  • Matrix Format Conversion: Transforms MATLAB matrices like [1,2;3,4] into Python-style [[1, 2], [3, 4]].
  • Variable Extraction: Identifies all variable names used in a matrix expression, excluding common math functions.
  • Matrix Assignment Parsing: Extracts both variable name and raw matrix string from MATLAB-style assignment lines.
  • Torch-Compatible Output: Provides a full example that outputs torch.tensor(...) objects from MATLAB expressions.

Files Overview

index_converter.py

Converts varname(n) to varname[n-1] using regex. Useful for transforming MATLAB indexing syntax.

convert_idx_for_var(expr: str, varname: str) -> str

matrix_wrapper_converter.py

Converts MATLAB matrix string format [a,b;c,d] to Python-style nested lists.

matlab_matrix_to_python(matlab_str: str) -> str

variable_extractor.py

Parses a matrix string and extracts all variable names (excluding built-ins like sin, cos, etc.).

extract_matlab_variable_names(matrix_str: str) -> list[str]

extract_raw_matrix_str.py

Extracts the variable name and raw matrix string from an input line such as:

M = [1, 2; 3, 4]

Even supports anonymous matrices (=[...] or [...]).

extract_assignment_and_matrix(input_str: str) -> tuple

general.py

Takes in string of Matlab matrix and return string of the matrix represented as a tensor:

M = [1, 2; 3, 4]

Even supports anonymous matrices (=[...] or [...]).

matlab_to_tensor(input_str: str) -> str

example.py

A full demo that:

  1. Extracts the matrix.
  2. Replaces MATLAB operations (e.g., .*, .^).
  3. Converts trigonometric functions to torch. variants.
  4. Converts indexing, and matrix format.
  5. Returns a torch.tensor(...) result.

Example Usage

from mat2py_matrix_converter import matlab_to_tensor, extract_matlab_variable_names

matlab_code = "M = [1, 2; 3, 4];"
vars = extract_matlab_variable_names(matlab_code)
py_tensor = matlab_to_tensor(matlab_code)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mat2py_matrix_converter-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mat2py_matrix_converter-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file mat2py_matrix_converter-0.1.0.tar.gz.

File metadata

  • Download URL: mat2py_matrix_converter-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for mat2py_matrix_converter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1c357aec7c440c9dda5c6b791eb93a84bd2af88b9b85128b35726f6c0bd0ce46
MD5 d6a52d55cb96a6e08ea41b352eee29fa
BLAKE2b-256 77bff736627ad45a4da68520eff46d51536502e5ecdb9c8d43d6ec2246b25450

See more details on using hashes here.

File details

Details for the file mat2py_matrix_converter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mat2py_matrix_converter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9969275aa046a9edabc435b1d4375e926092aa8de3378a3e86139b3c26a1b887
MD5 4b412e85077b51003ce51673636e32f2
BLAKE2b-256 a18234a77de34a65b86998f8524e8791a0dbcdce4d6286df1fce492b76ce5cc7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page