# Write Matlab Functions to Python

# Overview

This article was also published on the site: https://dzone.com/articles/write-matlab-functions-to-python.

Recently in my work I was re-writing algorithms developed in MatLab to Python, some functions are not so simple to adapt, especially the array functions that are called Cell Arrays.

MatLab has an API where you can call via Python the functions of the MatLab itself, but the idea was not to use MatLab, but the same algorithm works the same way using only Python and NumPy, the GNU Octace also has an API similar to that of MatLab.

To maintain compatibility I have created functions with the same name that are used in MatLab that is encapsulated in a class called Precision.

## 1. Testing

Make the repository clone and follow the instructions in the README file:

Below I will show some examples, these are contained in the unit tests.

### 1.1 Start stopwatch time

Measuring the time spent in processing.

The output will look something like this:

### 1.2 Percentiles of a data set

Used to get a percentile. Example by creating a range of ordinal dates by cutting 5% from the left and 5% from the right.

The output will look something like this:

### 1.3 Cell Array (cell2mat)

Convert cell array to ordinary array of the underlying data type.

The output will look something like this:

### 1.4 Cell Array (num2cell)

Convert array to cell array with consistently sized cells.

The output will look something like this:

### 1.5 Concatenate strings (strcat)

Concatenate strings horizontally using strcat.

The output will look something like this:

### 1.6 Histogram (histc)

Counts the number of values in x that are within each specified bin range. The input, binranges, determines the endpoints for each bin. The output, bincounts, contains the number of elements from x in each bin.

The output will look something like this:

### 1.7 Unique

Looking for unique values in array and returning the indexes, inverse, and counts.

The output will look something like this:

### 1.8 Overlaps

Looking for the overlays between two arrays returning the index.

The output will look something like this:

## Considerations

There are functions that are not exactly MatLab but will serve as support, I hope it can help someone. There is an interesting article in NumPy for users who are migrating from MayLab to Python.

## Leave a Comment