srkdb
srkdb

Reputation: 815

Repmat in Python v/s MATLAB

For a given matrix, I'm subtracting the maximum of each row from the row. In MATLAB, I'd do something like

Xm = X - repmat(max(X,[],2), 1, size(X,2));

For Python, I've been toying with variations of

Xm = X - np.matlib.repmat(np.max(X,axis = 1), 1, len(X[0])) but to no avail!

Where exactly am I missing out? Thanks a lot.

Upvotes: 1

Views: 783

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60660

In Python/NumPy, explicitly replicating an array to make it match another is not necessary. Dimensions of size 1 (singleton dimensions) are automatically expanded in a process they call "broadcasting".

For example, given

X = np.array([[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11]])

you can write

X - np.max(X, axis=0)

The array returned by np.max has a shape of (5,), which is compatible with the shape of X ((3, 5)). NumPy aligns the shapes starting at the last dimension, and fills in singleton dimensions at the front if one array has fewer than the other.

However, this process sometimes needs a little bit of help. Because np.max returns an array with fewer dimensions than the input, broadcasting cannot, in general. match up these arrays. For example, X - np.max(X, axis=1) doesn't work.

The best solution is to use the keepdims argument to np.max:

X - np.max(X, axis=1, keepdims=True)

The other option is to add singleton dimensions using np.newaxis in indexing:

X - np.max(X, axis=1)[:, np.newaxis]

Upvotes: 1

Related Questions