scipy matlab到python代码转换

ijxebb2r  于 2023-03-30  发布在  Matlab
关注(0)|答案(2)|浏览(169)

由于缺少matlab,我正在尝试将matlab代码转换为python。如果您能告诉我以下函数的python等效函数,我将不胜感激:

letter2number_map('A') = 1;
number2letter_map(1) = 'A';
str2num()

strcmp()
trace()
eye()
getenv('STRING')

[MI_true, ~, ~] = function() What does ~ mean?
mslice
ones()

非常感谢您的帮助。

dz6r00yl

dz6r00yl1#

我其实不知道matlab,但我可以回答其中的一些问题(假设你已经将numpy导入为np):

letter2number_map('A') = 1;  -> equivalent to a dictionary:  {'A':1}    
number2letter_map(1) = 'A';  -> equivalent to a dictionary:  {1:'A'}

str2num() -> maybe float(), although a look at the docs makes 
             it look more like eval -- Yuck. 
             (ast.literal_eval might be closest)
strcmp()  -> I assume a simple string comparison works here.  
             e.g. strcmp(a,b) -> a == b
trace()   -> np.trace()    #sum of the diagonal
eye()     -> np.eye()      #identity matrix (all zeros, but 1's on diagonal)
getenv('STRING')  -> os.environ['STRING'] (with os imported of course)
[MI_true, ~, ~] = function() -> Probably:  MI_true,_,_ = function()  
                                although the underscores could be any  
                                variable name you wanted. 
                                (Thanks Amro for providing this one)
mslice    -> ??? (can't even find documentation for that one)
ones()    -> np.ones()     #matrix/array of all 1's
sczxawaw

sczxawaw2#

从字母转换为数字:ord("a") = 97
从数字转换为字母:chr(97) = 'a'
(You可以减去96来得到你想要的结果,对于小写字母,或者对于大写字母,减去64。
将字符串解析为int:int("523") = 523
比较字符串(区分大小写):"Hello"=="Hello" = True
不区分大小写:"Hello".lower() == "hElLo".lower() = True
ones()[1]*ARRAY_SIZE
单位矩阵:[[int(x==y) for x in range(5)] for y in range(5)]
要创建二维数组,需要使用numpy
编辑:
或者,你可以像这样制作一个5x5的二维数组:[[1 for x in range(5)] for y in range(5)]

相关问题