To express Matlab 3D matrix in Python

Asked 2 years ago, Updated 2 years ago, 62 views


    # Matlab
    f = zeros(984,17,10); # (x,y,z)
    a = squeeze(f(1,:,:));
    >> size(a) is (17,10)

    # Python
    f = np.zeros((10, 984, 17));  #(z,x,y)
    a = np.squeze(f[:,1,:]);
    >> a.shape is (10,17)

As we tried to convert the matrap expression to Python, we transformed the three-dimensional form like (x,y,z) -> (z,x,y). The problem is when you use squeeze. It should be 17, 10 as in Matlab. Reshape changes the order of the contents and changes the result value.

Is there any way to solve this problem?

python matlab

2022-09-20 19:45

2 Answers

.T to obtain the transposition.

Please refer to the code below.

import numpy as np

f = np.zeros((10, 984, 17))
a = np.squeeze(f[:,1,:]).T

print(a.shape)


2022-09-20 19:45

Please refer to the code below.

f=zeros(10,5,2);
f(1,:,:)=[1 2; 3 4; 5 6; 7 8; 9 10];
a=squeeze(f(1,:,:));

a

# -*- coding: utf-8 -*-

import numpy as np

f = np.zeros((2, 10, 5))
f[:,1,:]=np.array([[1, 2],[3, 4],[5, 6],[7, 8],[9, 10]]).T
a = np.squeeze(f[:,1,:]).T

print(a.shape)
print(a)


2022-09-20 19:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.