【问题标题】:How to convert this Octave/Matlab code to Python 3.7.4?如何将此 Octave/Matlab 代码转换为 Python 3.7.4?
【发布时间】:2020-05-31 09:50:46
【问题描述】:

我有一些正在工作的 Octave/Matlab 代码,我正在尝试使其工作/转换为 Python 3.7.4,因此我可以在 使用 Python 3.7.4 的 Blender 2.82 中使用它。

工作的 Octave/Matlab 代码是:

c=[7,-2,-4,-8]

[rw col]= size(c); %get size a array

num_of_loops=5 %number of loops to iterate
a= zeros(num_of_loops,col); %allocate memory to array
b= zeros(1,(rw*col)); %allocate memory to array

a(1,:)=c; %add c array to 1st row in array 

for n=1:num_of_loops
  n
  a = c .+ [c(end).*(0:4)].'; 
  b = vec (a.', 2);
endfor

b=reshape(a',1,[]);

这给了我正确的输出:

c= 
7   -2  -4  -8

a=
 7  -2  -4  -8
-1  -10 -12 -16
-9  -18 -20 -24
-17 -26 -28 -32
-25 -34 -36 -40

b=
7   -2  -4  -8  -1  -10 -12 -16 -9  -18 -20 -24 -17 -26 -28 -32 -25 -34 -36 -40 

如果你需要 if / then / else 命令这里是original question。)

我尝试了online convert octave/matlab to python converter,它给了我下面的代码(它不能完全工作)。如何修复 Python 3.x 代码以使其在使用 Python 3.7.4 的 Blender 2.82 中工作?

c = mcat([7, -2, -4, -8]); print c

[rw, col] = size(c)#get size a array

num_of_loops = 5; print num_of_loops#number of loops to iterate
a = zeros(num_of_loops, col)#allocate memory to array
b = zeros(1, (rw * col))#allocate memory to array

a(1, mslice[:]).lvalue = c#add c array to 1st row in array

for n in mslice[1:num_of_loops]:
    n()
    mcat([c(end) *elmul* (mslice[0:4])]).T
    b = vec(a.T, 2)
    end

    b = reshape(a.cT, 1, mcat([]))

【问题讨论】:

    标签: python-3.x matlab octave blender


    【解决方案1】:

    可以将给定的 MATLAB/Octave 代码最小化(另请参阅previous question 上的评论)到:

    c = [7, -2, -4, -8]
    a = c .+ [c(end).*(0:4)].'
    b = vec(a.', 2)
    

    对应的输出是:

    c =
       7  -2  -4  -8
    
    a =
        7   -2   -4   -8
       -1  -10  -12  -16
       -9  -18  -20  -24
      -17  -26  -28  -32
      -25  -34  -36  -40
    
    b =
        7 -2 -4 -8 -1 -10 -12 -16 -9 -18 -20 -24 -17 -26 -28 -32 -25 -34 -36 -40
    

    使用NumPy 可以轻松地将 MATLAB/Octave 代码传输到 Python。有很多关于“NumPy for MATLAB users”的资源可以在网上找到,例如this 一位来自开发者自己。

    我的解决方案如下所示:

    import numpy as np
    
    c = np.array([[7, -2, -4, -8]])
    a = c + (np.expand_dims(c[0][-1] * np.arange(5), 1))
    b = a.reshape(1, np.prod(a.shape))
    
    # Just for console output 
    print('c = ')
    print(c, '\n')
    print('a = ')
    print(a, '\n')
    print('b = ')
    print(b, '\n')
    

    该代码的输出是:

    c = 
    [[ 7 -2 -4 -8]] 
    
    a = 
    [[  7  -2  -4  -8]
     [ -1 -10 -12 -16]
     [ -9 -18 -20 -24]
     [-17 -26 -28 -32]
     [-25 -34 -36 -40]] 
    
    b = 
    [[  7 -2 -4 -8 -1 -10 -12 -16 -9 -18 -20 -24 -17 -26 -28 -32 -25 -34 -36 -40]] 
    

    您可以自行检查您的环境是否支持 NumPy。如果不是,我想,解决方案可能会变得复杂得多。

    希望对您有所帮助。

    ----------------------------------------
    System information
    ----------------------------------------
    Platform:    Windows-10-10.0.16299-SP0
    Python:      3.8.1
    NumPy:       1.18.1
    Octave:      5.1.0
    ----------------------------------------
    

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 1970-01-01
      • 2022-12-30
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      相关资源
      最近更新 更多