簡而言之,我怎樣才能將這 1 行代碼更改reshape((b-c)/d,1,[]) 為 python?
>> a=1:20;
>> b=reshape(a,4,5)
b =
1 5 9 13 17
2 6 10 14 18
3 7 11 15 19
4 8 12 16 20
>> c=-3;
>> d=2;
>> reshape((b-c)/d,1,[])
ans =
Columns 1 through 17
2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
Columns 18 through 20
10.5000 11.0000 11.5000
uj5u.com熱心網友回復:
我認為您正在尋找以下內容:
import numpy as np
a = np.arange(1,21)
b = a.reshape(5,4)
c = -3
d = 2
ans = ((b-c)/d).flatten()
# array([ 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. ,
# 7.5, 8. , 8.5, 9. , 9.5, 10. , 10.5, 11. , 11.5])
如果您設定b為問題中所示的形狀,則需要轉置兩次(否則沒有必要)。IE:
a = np.arange(1,21)
b = a.reshape(5,4).transpose()
array([[ 1, 5, 9, 13, 17],
[ 2, 6, 10, 14, 18],
[ 3, 7, 11, 15, 19],
[ 4, 8, 12, 16, 20]])
c = -3
d = 2
ans = ((b-c)/d).transpose().flatten()
# array([ 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. ,
# 7.5, 8. , 8.5, 9. , 9.5, 10. , 10.5, 11. , 11.5])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511565.html
標籤:Pythonmatlab
上一篇:python中Matlabnan()的等價物是什么?[復制]
下一篇:如何更改不同子圖的圖例字串?
