日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

MATLAB使用Python数值和字符变量

發(fā)布時間:2025/3/21 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MATLAB使用Python数值和字符变量 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在 MATLAB 中使用 Python 數(shù)值類型

當(dāng)調(diào)用接受數(shù)值輸入?yún)?shù)的 Python 函數(shù)時,MATLAB 會將雙精度值轉(zhuǎn)換為最適合在 Python 語言中表示該數(shù)據(jù)的類型。例如,要調(diào)用 Python math?模塊中的三角函數(shù),請傳遞 MATLAB 雙精度值。

pynum = py.math.radians(90)pynum = 1.5708

對于返回 Python?float?類型的函數(shù),MATLAB 會自動將該類型轉(zhuǎn)換為雙精度類型。???????

class(pynum)ans = 'double'

對于返回整數(shù)類型的 Python 函數(shù),MATLAB 會自動將該類型轉(zhuǎn)換為?int64。例如,bit_length?函數(shù)返回將二進(jìn)制整數(shù)表示為?int?值所需的位數(shù)。???????

py.int(intmax).bit_lengthans = int64 31

用數(shù)值?iterable?參數(shù)調(diào)用 Python 方法

Python?math.fsum?函數(shù)對?iterable?輸入?yún)?shù)中的浮點值求和。例如,打開 MATLAB patients.mat?數(shù)據(jù)文件并讀取數(shù)值數(shù)組?Height。???????

load patients.matclass(Height)ans = 'double' size(Height)ans = 1×2 100 1

當(dāng)將此參數(shù)傳遞給 Python 時,MATLAB 自動將數(shù)值轉(zhuǎn)換為 Python 數(shù)值且 Python 會對向量值進(jìn)行迭代。???????

py.math.fsum(Height)ans = 6707

在 MATLAB 中使用 Python?array?類型

假設(shè)有一個 Python 函數(shù),它返回以下雙精度類型的 Python?array.array。???????

P = py.array.array('d', 1:5)P = Python array with properties: itemsize: 8 typecode: [1×1 py.str] array('d', [1.0, 2.0, 3.0, 4.0, 5.0])

要將?P?傳遞給 MATLAB 函數(shù)?sum,請將?P?轉(zhuǎn)換為雙精度類型的 MATLAB 數(shù)組。???????

>> sum(P)錯誤使用 sum數(shù)據(jù)類型無效。第一個參數(shù)必須為數(shù)值或邏輯值。sum(double(P))ans = 15

在 MATLAB 中使用 Python 整數(shù)?array?類型

假設(shè)有以下 Python 數(shù)組。對該數(shù)組調(diào)用 Python reverse?函數(shù),然后將結(jié)果轉(zhuǎn)換為 MATLAB 數(shù)組。???????

arr = py.array.array('i',[int32(5),int32(1),int32(-5)])arr = Python array with properties: itemsize: 4 typecode: [1×1 py.str] array('i', [5, 1, -5])arr.reverseA = int32(arr)A = 1×3 int32 row vector -5 1 5

默認(rèn)數(shù)值類型

默認(rèn)情況下,MATLAB 中的數(shù)值是?double?類型。默認(rèn)情況下,Python 中的數(shù)值(沒有小數(shù)部分)是整數(shù)類型。這種差異會導(dǎo)致在將數(shù)值傳遞給 Python 函數(shù)時出現(xiàn)混淆。

例如將下列 MATLAB 數(shù)值傳遞給 Python?datetime?函數(shù)時,Python 會將它們讀取為?float?類型并顯示錯誤:???????

d = py.datetime.date(2014,12,31)Python Error: TypeError: integer argument expected, got float

要更正該錯誤,請將每個數(shù)值顯式轉(zhuǎn)換為整數(shù)類型:???????

d = py.datetime.date(int32(2014),int32(12),int32(31))d = Python date with properties: day: 31 month: 12 year: 2014 2014-12-31

?

總結(jié)

以上是生活随笔為你收集整理的MATLAB使用Python数值和字符变量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。