亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 scipy.io savemat 將多個 Python 字典轉換為 MATLAB 結構體數組

使用 scipy.io savemat 將多個 Python 字典轉換為 MATLAB 結構體數組

楊魅力 2022-11-01 17:03:57
一個簡單的問題,但我似乎無法理解。我正在使用該scipy.io庫將 Python 字典保存為 Matlab 結構。現在,該庫的文檔scipy.io向我們展示了如何將單個 Python 字典轉換為單個 Matlab 結構:>>> a_dict = {'field1': 0.5, 'field2': 'a string'} >>> sio.savemat('saved_struct.mat', {'a_dict': a_dict})這聽起來很公平,并且有效:但是,我現在想對多個 Python 字典執行相同的操作。我希望將它們轉換為 Matlab 結構,其中列名等于所有字典的鍵(顯然都是相同的鍵名),并且我希望每一行代表其中一個鍵的值字典。如果我沒看錯,這稱為 1 x K 結構,有 10 個字段,其中 K 是我要映射的行數(Python 字典)。字段示例如下所示:盡管我自己完全不知道正確的 Matlab 術語,但評論中的一個好人告訴我這應該被稱為結構數組。我嘗試簡單地創建一個 Python 字典的 numpy 數組,將其放在a_dict上面代碼示例的鍵值對中并保存,但沒有成功。這樣做會導致所有不同結構的列表,而不是一個大結構,其中行表示每個單獨結構的值。因此,我仍在為這個問題尋找合適的解決方案。如果您需要任何其他詳細信息,請隨時在評論中提問。感謝您的幫助!
查看完整描述

2 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

這是一個解決方案:


在 Python 中:


>>> a_dict = {'field1': 0.5, 'field2': 'a string'}

>>> b_dict = {'field1': 1, 'field2': 'another string'}

>>> sio.savemat('saved_struct.mat', {'dict_array':[a_dict,b_dict]})

在 MATLAB 中:


s = load('saved_struct.mat');

struct_array = [s.dict_array{:}];

您將根據需要在 MATLAB 中得到一個結構體數組。


struct_array = 


  1×2 struct array with fields:


    field1

    field2


查看完整回答
反對 回復 2022-11-01
?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

為了澄清structured array建議,我將舉一個例子。


定義一個結構化數組:


In [192]: arr = np.array([(0.5,'one'),(0.6,'two'),(0.8,'three')], dtype=[('field1',float),('field2','U10')])                                                                                        

以及具有相同字段和數據的字典列表:


In [194]: dicts = [{'field1':0.5, 'field2':'one'},{'field1':0.6, 'field2':'two'},{'field1':0.8,'field2':'three'}]


In [195]: arr                                                                                          

Out[195]: 

array([(0.5, 'one'), (0.6, 'two'), (0.8, 'three')],

      dtype=[('field1', '<f8'), ('field2', '<U10')])


In [196]: dicts                                                                                        

Out[196]: 

[{'field1': 0.5, 'field2': 'one'},

 {'field1': 0.6, 'field2': 'two'},

 {'field1': 0.8, 'field2': 'three'}]

保存和加載:


In [197]: io.savemat('ones.mat', {'arr':arr, 'dicts':dicts})                                           

In [198]: io.loadmat('ones.mat')                                                                       

Out[198]: 

{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Fri May  1 09:06:19 2020',

 '__version__': '1.0',

 '__globals__': [],

 'arr': array([[(array([[0.5]]), array(['one'], dtype='<U3')),

         (array([[0.6]]), array(['two'], dtype='<U3')),

         (array([[0.8]]), array(['three'], dtype='<U5'))]],

       dtype=[('field1', 'O'), ('field2', 'O')]),

 'dicts': array([[array([[(array([[0.5]]), array(['one'], dtype='<U3'))]],

       dtype=[('field1', 'O'), ('field2', 'O')]),

         array([[(array([[0.6]]), array(['two'], dtype='<U3'))]],

       dtype=[('field1', 'O'), ('field2', 'O')]),

         array([[(array([[0.8]]), array(['three'], dtype='<U5'))]],

       dtype=[('field1', 'O'), ('field2', 'O')])]], dtype=object)}

savemat創建了一些對象 dtype 數組(和字段)和 2d MATLAB 類數組。


在 Octave 會話中:


>> load ones.mat

這arr是一個struct array有2個字段:


>> arr

arr =


  1x3 struct array containing the fields:


    field1

    field2


>> arr.field1

ans =  0.50000

ans =  0.60000

ans =  0.80000

>> arr.field2

ans = one

ans = two

ans = three

dicts是具有標量結構的單元:


>> dicts

dicts =

{

  [1,1] =


    scalar structure containing the fields:


      field1 =  0.50000

      field2 = one


  [1,2] =


    scalar structure containing the fields:


      field1 =  0.60000

      field2 = two


  [1,3] =


    scalar structure containing the fields:


      field1 =  0.80000

      field2 = three


}

可以轉換為與@Unbearable 顯示的相同的結構數組:


>> [dicts{:}]

ans =


  1x3 struct array containing the fields:


    field1

    field2


>> _.field1

error: '_' undefined near line 1 column 1

>> [dicts{:}].field1

ans =  0.50000

ans =  0.60000

ans =  0.80000

>> [dicts{:}].field2

ans = one

ans = two

ans = three


查看完整回答
反對 回復 2022-11-01
  • 2 回答
  • 0 關注
  • 312 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號