如何将此python代码转换为matlab脚本?

2024-06-10 22:26:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试将python代码转换为Matlab脚本,但我确实需要一些帮助。我对编程世界相当陌生,因为我不知道不同的命令

python code

我曾试图逐字转换,但我失败得很惨:(

def ranking(A):
     import numpy as np
     from mat1120lib import null

 if (A.shape[0] == A.shape[1] and len(A.shape) == 2):
     n = A.shape[0]
 else:
     n=0
     print "A is not a square matrix,aborting!"
     import sys
     sys.exit(1)
 tol = 1e-12
 for i in range(n);
     if (np.sum(A.T[i])-)>tol:
         print "A is not a stochastic matrix, aborting!"
         import sys
         sys.exit(1)
 S=1/float(n) * np.ones((n,n))
 m=0.15
 M=(1-m)*A + m*S
 n=null(M-np.eye(n))
 return n/np.sum(n)

Tags: 代码importifisnpsysexitnot
3条回答

下面是在MATLAB中复制功能的粗略尝试。如另一个答案所述,可以获得排名

A = [1/3 1/3 1/3; 1/3 1/3 1/3; 1/3 1/3 1/3];

%Checking if A is a square matrix%
[Number_Of_Rows, Number_Of_Columns] = size(A);

if (Number_Of_Rows ~= Number_Of_Columns)
fprintf("A is not a square matrix, aborting!\n");  
end

%Checking if the A is a stochastic matrix%
Total_Probability = 1;

Stochatic_Flag = 0;
Valid_Columns = 0;
for Column_Index = 1: +1: Number_Of_Columns
    
Current_Column = A(:,Column_Index);

if(sum(Current_Column,'all') == 1)
    Valid_Columns = Valid_Columns + 1;
end 

end


if(Valid_Columns == Number_Of_Columns)
    Stochatic_Flag = 1;
end

if(Stochatic_Flag == 0) 
    fprintf("A is not a stochastic matrix.\n")
end

N = Number_Of_Columns;
S = 1/N * ones(N,N);
m = 0.15;
M = (1-m)*A + m*S;

如果你真的想得到矩阵的秩,你可以简单地使用Matlab内置函数

rank(A)

正如您所说,逐行翻译代码是一种可能性,在我看来,由于图片上的函数相对较短,可能是解决问题的最快方法

关于为什么源代码转换器不那么常见,请检查以下线程: Why aren't there automated translators from one programming language to another? [closed]

Edit01:我对整段代码进行了注释,并解释了每行代码的作用,如下所示。我希望它能帮助您理解Python中的代码

# This is defining the function "ranking", which receives the "A" argument
def ranking(A):
    
    # This imports the library/package NumPy
    import numpy as np
    
    # This imports the "null" function/type from the library/package mat1120lib; (this library was probably given to you by your teacher)
    from mat1120lib import null
    
    # This will check if the first and the second dimension of "" are equal, and check if the number of dimensions is two
    if (A.shape[0] == A.shape[1] and len(A.shape) == 2):
        # If so, it means "A" is a square array/matrix, so "n" gets the dimension of the size of the square
        n = A.shape[0]
    else:
        # Otherwise, "n" gets zero, you get a prompt and it aborts
        n=0
        print "A is not a square matrix,aborting!"
        import sys
        sys.exit(1)
    
    # You set tolerance "tol" to 10^-12
    tol = 1e-12
    
    # You start a for loop
    for i in range(n);
        # This checks if the sum of the elements of "A.T" in position "i" is greather than the tolerance "tol"
        if (np.sum(A.T[i])-)>tol:
            # If so, you get a prompt and it aborts
            print "A is not a stochastic matrix, aborting!"
            import sys
            sys.exit(1)
    
    # "S" becomes a matrix of n-by-n elements, with which elements being 1/n
    S=1/float(n) * np.ones((n,n))

    m=0.15
    
    # "M" gets 0.85*A + 0.15*S
    M=(1-m)*A + m*S
    
    # Uses the output of the null function to overwrite the "n" variable
    n=null(M-np.eye(n))
    
    # The function ends and returns the result of this operation
    return n/np.sum(n)
 

相关问题 更多 >