change matlab linear index to numpy.

5 ビュー (過去 30 日間)
Sure
Sure 2023 年 6 月 3 日
回答済み: Kautuk Raj 2023 年 6 月 3 日
I have a three-dimensional matrix X with dimensions 16 x 10 x 1200. The original program used
for i = 3876:10000
My(:,:,i) = X(:,i)*X(:,i)';
end
Obviously, this program uses linear indexing in Matlab. I have read the relevant documentation for Matlab and NumPy, but I still do not know how to convert it to the corresponding operation in NumPy. Can anyone help me? Thank you very much.

回答 (1 件)

Kautuk Raj
Kautuk Raj 2023 年 6 月 3 日
This can be the equivalent NumPy code for the given MATLAB code:
import numpy as np
X = np.random.rand(16, 10, 1200)
My = np.zeros((16, 16, 10000-3876+1))
for i in range(3876, 10001):
My[:, :, i-3876] = np.outer(X[:, i-1], X[:, i-1])
Note that we use i-1 as the index for X since Python uses 0-based indexing.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by