欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在matlab中創(chuàng)建類似字典的數(shù)據(jù)結(jié)構(gòu)方式

 更新時間:2023年03月25日 15:15:55   作者:*Major*  
這篇文章主要介紹了在matlab中創(chuàng)建類似字典的數(shù)據(jù)結(jié)構(gòu)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

matlab中創(chuàng)建類似字典的數(shù)據(jù)結(jié)構(gòu)

Matlab中創(chuàng)建struct:

d = struct('a','1','b','2')

在Matlab中通過字符串形式的fieldname來查找value(Using Dynamic FielNames):

d.('a')
d.('b')

在Matlab中修改已經(jīng)存在的field的value:

d.('b')='9'

在Matlab中添加新的field:

d.('c')='3'

在Matlab中刪除stuct的field:

d=struct('b', '9','c', '3', 'a', '1')
d=rmfield(b,'9')

在Matlab中刪除struct所有field:

d=struct()

matlab基本數(shù)據(jù)結(jié)構(gòu)說明

Matlab作為早期的科研軟件之一,對數(shù)據(jù)的處理有強大的功能。最近需要做信號處理相關的部分,將之前對Matlab不熟悉的數(shù)據(jù)操作熟悉一下。

最后準備附上Numpy中對兩者之間基本操作的比較。

1、基本數(shù)據(jù)結(jié)構(gòu)總覽(僅含常用類型)

matlab基本數(shù)據(jù)結(jié)構(gòu)

2、數(shù)值類型基本操作

2.1 數(shù)據(jù)變量的創(chuàng)建

doubleMatlab的默認數(shù)據(jù)類型,基本上可以滿足大部分的計算任務。single和double的創(chuàng)建方式一致

%% 創(chuàng)建數(shù)組
x = 10;
Y = double(x);  % 轉(zhuǎn)換為double型數(shù)組
y = 10;

%% 查看數(shù)據(jù)類型
class(Y)

2.2 查看數(shù)據(jù)類型和值

函數(shù)名稱函數(shù)功能
isinteger確定輸入是否為整數(shù)數(shù)組
isfloat確定輸入是否為浮點數(shù)組
isnumeric確定輸入是否為數(shù)值數(shù)組
isnan確定那些數(shù)組為NaN
class查看數(shù)據(jù)類型

注:以上函數(shù)的返回值均為0(結(jié)果為假)或1(結(jié)果為真)

3、字符和字符串

3.1 字符串創(chuàng)建

% 創(chuàng)建字符串
str = "Hello, world"
>>> str = 
"Hello, world"

% 創(chuàng)建字符串數(shù)組
str = ["Mercury" "Gemini" "Apollo";
       "Skylab" "Skylab B" "ISS"]
       
>>> str = 2x3 string
    "Mercury"    "Gemini"      "Apollo"
    "Skylab"     "Skylab B"    "ISS" 
  
% 傳入?yún)?shù)轉(zhuǎn)換為字符串數(shù)組
str = string(A)   % 將輸入的數(shù)組轉(zhuǎn)換為字符串數(shù)組
str = string(A, datetFmt)   % 將傳入的A轉(zhuǎn)換指定的時間格式,所以傳入的A是datetime或者duration

A的輸入格式?jīng)Q定了如何將A轉(zhuǎn)換字符串數(shù)組

輸入類型轉(zhuǎn)換說明示例輸入示例輸出
char每一行變?yōu)樽址囊粋€標量1×3 char array 'foo'1×1 string array "foo"
cell元胞數(shù)組的每一個元素都轉(zhuǎn)換為1 * 1的字符串{137,'foo'}["137" "foo"]
數(shù)值數(shù)組相當于compose的%g格式[137 3.1e-3 8.5e-6]["137" "0.0031" "8.5e-06"]
datetime要指定格式和區(qū)域設置datetime(2020,6,1)"01-Jun-2020"
邏輯數(shù)組logical 函數(shù)不接受字符串輸入,因此轉(zhuǎn)換是單向的。logical([0 1])["false" "true"]

注:在matlab中要區(qū)分字符和字符串的差異。

示例:

% A表示的是字符向量
A = 'Four score and seven years ago'
c = size(A)   
>>> c = 1×2

     1    30
str = string(A)
s = size(str)
>>> s = 1×2

     1     1
% 通過strlength可求的str中字符串的長度
n = strlength(str)
>>> n = 30


% 轉(zhuǎn)換元胞數(shù)組
A = {'Mercury','Gemini','Apollo';...
     'Skylab','Skylab B','ISS'}
>>> A = 2x3 cell
    {'Mercury'}    {'Gemini'  }    {'Apollo'}
    {'Skylab' }    {'Skylab B'}    {'ISS'   }
str = string(A)
>>> str = 2x3 string
    "Mercury"    "Gemini"      "Apollo"
    "Skylab"     "Skylab B"    "ISS" 
    
% 轉(zhuǎn)換表示數(shù)字的字符串
str = ["256","3.1416","8.9e-3"]
>>> str = 1x3 string
    "256"    "3.1416"    "8.9e-3"
X = double(str)
>>> X = 1×3

  256.0000    3.1416    0.0089
Y = str2double(str)
>>>Y = 1×3

  256.0000    3.1416    0.0089

注:

1、erase可以刪除字符串中的指定符號

2、str2double適用于輸入?yún)?shù)可能是字符串數(shù)組、字符向量或字符向量元胞數(shù)組,本質(zhì)上說明str2double對字符串的轉(zhuǎn)換有更加廣泛的適用

3.2 字符串數(shù)組的訪問

按照數(shù)組訪問的方式對字符串進行訪問,可通過下標的方式對數(shù)組進行切片和訪問。

4、結(jié)構(gòu)體

結(jié)構(gòu)體數(shù)組是使用名為字段的數(shù)據(jù)容器將相關數(shù)據(jù)組合在一起的數(shù)據(jù)類型。每個字段都可以包含任意類型的數(shù)據(jù)。可以使用structName.fieldName 格式的圓點表示法來訪問字段中的數(shù)據(jù)。

形式上和Python的字典的創(chuàng)建方式類似。但是數(shù)組的維度取決于對應值的形式,通過字段名的方式對相應的value進行訪問。

4.1 創(chuàng)建結(jié)構(gòu)體

%% 創(chuàng)建一個字段的結(jié)構(gòu)體
field = 'f';
value = {'some text';
         [10, 20, 30];
         magic(5)};
s = struct(field,value)

% 查看每個元素在內(nèi)容
>>> ans = 
'some text'
ans = 1×3

    10    20    30

ans = 5×5

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

%% 創(chuàng)建多個字段的結(jié)構(gòu)體
field1 = 'f1';  value1 = zeros(1,10);
field2 = 'f2';  value2 = {'a', 'b'};
field3 = 'f3';  value3 = {pi, pi.^2};
field4 = 'f4';  value4 = {'fourth'};

s = struct(field1,value1,field2,value2,field3,value3,field4,value4)
>>> s=1×2 struct array with fields:
    f1
    f2
    f3
    f4
% 輸出上述字段的結(jié)果
>>> s(1)
ans = struct with fields:
    f1: [0 0 0 0 0 0 0 0 0 0]
    f2: 'a'
    f3: 3.1416
    f4: 'fourth'
>>> s(2)
ans = struct with fields:
    f1: [0 0 0 0 0 0 0 0 0 0]
    f2: 'b'
    f3: 9.8696
    f4: 'fourth'

%% 創(chuàng)建空字段的結(jié)構(gòu)體
s = struct('f1','a','f2',[])
>>> s = struct with fields:
    f1: 'a'
    f2: []
%% 創(chuàng)建包含元胞數(shù)組的字段
field = 'mycell';
value = {{'a','b','c'}};
s = struct(field,value)
>>> s = struct with fields:
    mycell: {'a'  'b'  'c'}

注:創(chuàng)建多個字段的結(jié)構(gòu)體時value2value3 的元胞數(shù)組是 1×2 數(shù)組,因此 s 也是 1×2 數(shù)組。因為 value1 是數(shù)值數(shù)組而不是元胞數(shù)組,所以 s(1).f1s(2).f1 具有相同的內(nèi)容。

類似地,因為 value4 的元胞數(shù)組具有單一元素,所以 s(1).f4s(2).f4 具有相同的內(nèi)容。

4.2 結(jié)構(gòu)體中的函數(shù)

函數(shù)名稱函數(shù)功能
struct創(chuàng)建按結(jié)構(gòu)體數(shù)組
fieldnames結(jié)構(gòu)體的字段名稱
getfield胡哦去結(jié)構(gòu)體數(shù)組字段
isfield確定輸入是否為結(jié)構(gòu)體數(shù)組字段
orderfields結(jié)構(gòu)體數(shù)組的字段順序
rmfield刪除結(jié)構(gòu)體中的字段
setfield為結(jié)構(gòu)體數(shù)組字段賦值
table2struct將表轉(zhuǎn)換為結(jié)構(gòu)體數(shù)組
struct2table將結(jié)構(gòu)體數(shù)組轉(zhuǎn)換為表
cell2struct將元胞數(shù)組轉(zhuǎn)換為結(jié)構(gòu)體數(shù)組
struct2cell將結(jié)構(gòu)體轉(zhuǎn)換為元胞數(shù)組

5 元胞數(shù)組

5.1 創(chuàng)建與訪問

創(chuàng)建空字符串

a = cell()
a = {}

5.2 轉(zhuǎn)換和檢查數(shù)據(jù)類型

從元胞轉(zhuǎn)換

函數(shù)名稱函數(shù)功能
cell2mat將元胞數(shù)組轉(zhuǎn)換為普通數(shù)組
cell2struct將元胞數(shù)組轉(zhuǎn)換為結(jié)構(gòu)體

注:

  • 元胞數(shù)組中的結(jié)構(gòu)體轉(zhuǎn)換為結(jié)構(gòu)體數(shù)組時,結(jié)構(gòu)體必須包含相同的字段(結(jié)構(gòu)體的類型有點像Python中的鍵值的方式,只是訪問的方式不同)

轉(zhuǎn)換為元胞

函數(shù)名稱函數(shù)功能
cellstr將字符向量轉(zhuǎn)變?yōu)樵麛?shù)組
mat2cell將數(shù)組轉(zhuǎn)換包含子數(shù)組的元胞數(shù)組.原始數(shù)組可通過參數(shù)dimNDist劃分為更小的數(shù)組。
num2cell將數(shù)組轉(zhuǎn)換為相同大小的元胞數(shù)組
struct2cell將結(jié)構(gòu)體轉(zhuǎn)換為元胞數(shù)組
table2cell將表轉(zhuǎn)換為元胞數(shù)組。元胞數(shù)組中不含字段名稱。通過fieldnames函數(shù)可以得到原始結(jié)構(gòu)體的字段名稱

5.3 元胞數(shù)組的操作

將元胞添加到元胞數(shù)組

C = {1, 2, 3}
>>> C=1×3 cell array
    {[1]}    {[2]}    {[3]}

C{4,4} = 44
>>> C=4×4 cell array
    {[       1]}    {[       2]}    {[       3]}    {0x0 double}
    {0x0 double}    {0x0 double}    {0x0 double}    {0x0 double}
    {0x0 double}    {0x0 double}    {0x0 double}    {0x0 double}
    {0x0 double}    {0x0 double}    {0x0 double}    {[      44]}

對元胞數(shù)組的訪問

  • 對元胞切片用圓括號()來訪問
  • 使用花括號{}對特定的內(nèi)容進行訪問
C = {'one', 'twC=2×3 cell array
    
>>> C=2×3 cell array
    {'one'}    {'two'}    {'three'}
    {[  1]}    {[  2]}    {[    3]}   

通過()切片

upperLeft = C(1:2,1:2)
>>> upperLeft=2×2 cell array
    {'one'}    {'two'}
    {[  1]}    {[  2]}

通過切片修改值

% 修改第1行 1,2,3列的值
C(1,1:3) = {'first','second','third'}
>>> C=2×3 cell array
    {'first'}    {'second'}    {'third'}
    {[    1]}    {[     2]}    {[    3]}

將元胞中的數(shù)值數(shù)據(jù)抓換為數(shù)值數(shù)組

numericCells = C(2,1:3)
>>> numericCells=1×3 cell array
    {[1]}    {[2]}    {[3]}
    
numericVector = cell2mat(numericCells)
>>> numericVector = 1×3

     1     2     3

通過{}對元胞的特定內(nèi)容訪問 / 或者修改特定內(nèi)容的值

% 訪問元胞的值
last = C{2,3}
>>> last = 3

C{2,3} = 300
>>> C=2×3 cell array
    {'first'}    {'second'}    {'third'}
    {[    1]}    {[     2]}    {[  300]}

訪問某一行的值并保存到數(shù)值數(shù)組中

% 訪問數(shù)組中某一行的值并轉(zhuǎn)換為數(shù)值數(shù)組
nums = [C{2,:}]
>>> nums = 1×3

     1     2   300

訪問元胞的多級索引

myNum = [1, 2, 3];
myCell = {'one', 'two'};
myStruct.Field1 = ones(3);
myStruct.Field2 = 5*ones(5);

C = {myNum, 100*myNum;
     myCell, myStruct}
     
>>> C=2×2 cell array
    {[ 1 2 3]}    {[100 200 300]}
    {1x2 cell}    {1x1 struct   }
    
% 訪問元胞的內(nèi)容
C{1,2}
>>> ans = 1×3

   100   200   300
  
c{1, 2}(1, 2)
>>> ans = 2

C{2,1}{1,2}
>>> ans = 'two'

注:

  • 將元胞添加到元胞數(shù)組內(nèi),如果當前的索引不存在,會自動對當前的數(shù)組擴容,不存在的用空元胞來代替
  • 通過追加索引,并使用與內(nèi)容的數(shù)據(jù)類型匹配的語法,來訪問元胞的部分內(nèi)容。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論