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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 循环神经网络 >内容正文

循环神经网络

matlab访问数组的元素,使用分类数组访问数据

發布時間:2024/9/27 循环神经网络 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 matlab访问数组的元素,使用分类数组访问数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

加載樣本數據

加載從 100 位患者收集的樣本數據。

load patients

whos

Name Size Bytes Class Attributes

Age 100x1 800 double

Diastolic 100x1 800 double

Gender 100x1 11412 cell

Height 100x1 800 double

LastName 100x1 11616 cell

Location 100x1 14208 cell

SelfAssessedHealthStatus 100x1 11540 cell

Smoker 100x1 100 logical

Systolic 100x1 800 double

Weight 100x1 800 double

基于字符向量元胞數組創建分類數組

Gender 和 Location 包含各類別中的數據。每個元胞數組都包含從一小組唯一值中獲取的字符向量(分別表示兩個性別和三個位置)。將 Gender 和 Location 轉換為分類數組。

Gender = categorical(Gender);

Location = categorical(Location);

搜索單個類別的成員

對于分類數組,可以使用邏輯運算符 == 和 ~= 來查找屬于或不屬于特定類別的數據。

確定是否存在到 'Rampart General Hospital' 位置就醫的任何患者。

any(Location=='Rampart General Hospital')

ans = logical

0

不存在到 Rampart General Hospital 就醫的患者。

搜索一組類別的成員

您可以使用 ismember 查找一組特定類別中的數據。為到 County General Hospital 或 VA Hospital 就醫的患者創建一個邏輯向量。

VA_CountyGenIndex = ...

ismember(Location,{'County General Hospital','VA Hospital'});

VA_CountyGenIndex 是一個 100x1 的邏輯數組,對于分類數組 Location 中屬于 County General Hospital 或 VA Hospital 類別的成員的每個元素,該數組都會在其對應位置包含邏輯值 true (1)。輸出 VA_CountyGenIndex 包含 76 個非零元素。

使用邏輯向量 VA_CountyGenIndex 選擇在 County General Hospital 或 VA Hospital 就醫的患者的 LastName。

VA_CountyGenPatients = LastName(VA_CountyGenIndex);

VA_CountyGenPatients 是一個 76×1 的字符向量元胞數組。

選擇特定類別中的元素以繪圖

使用 summary 函數可輸出一份包含每一類別的類別名稱及元素數的摘要。

summary(Location)

County General Hospital 39

St. Mary's Medical Center 24

VA Hospital 37

Location 是一個包含三個類別的 100×1 分類數組。County General Hospital 出現在 39 個元素中,St. Mary s Medical Center 出現在 24 個元素中,VA Hospital 出現在 37 個元素中。

使用 summary 函數輸出 Gender 的摘要。

summary(Gender)

Female 53

Male 47

Gender 是一個包含兩個類別的 100×1 分類數組。Female 出現在 53 個元素中,Male 出現在 47 個元素中。

使用邏輯運算符 == 僅訪問女性患者的年齡。然后利用這些數據繪制一個直方圖。

figure()

histogram(Age(Gender=='Female'))

title('Age of Female Patients')

histogram(Age(Gender=='Female')) 對 53 個女性患者的年齡數據繪圖。

刪除特定類別中的數據

您可以使用邏輯運算符包括或排除特定類別中的數據。刪除工作區變量 Age 和 Location 中到 VA Hospital 就醫的所有患者。

Age = Age(Location~='VA Hospital');

Location = Location(Location~='VA Hospital');

現在,Age 是一個 63×1 的數值數組,Location 是一個 63×1 的分類數組。

列出 Location 的類別以及每個類別中的元素數。

summary(Location)

County General Hospital 39

St. Mary's Medical Center 24

VA Hospital 0

到 VA Hospital 就醫的患者已從 Location 中刪除,但 VA Hospital 仍是一個類別。

使用 removecats 函數從 Location 的類別中刪除 VA Hospital。

Location = removecats(Location,'VA Hospital');

驗證 VA Hospital 類別是否已刪除。

categories(Location)

ans = 2x1 cell

{'County General Hospital' }

{'St. Mary's Medical Center'}

Location 是一個包含兩個類別的 63×1 分類數組。

刪除元素

您可以按索引刪除元素。例如,您可以通過使用 Location(2:end) 選擇其余元素來刪除 Location 的第一個元素。不過,更簡單的元素刪除方式是使用 []。

Location(1) = [];

summary(Location)

County General Hospital 38

St. Mary's Medical Center 24

Location 是一個包含兩個類別的 62×1 分類數組。刪除第一個元素對同一類別中的其他元素沒有任何影響,并且不會刪除該類別本身。

檢查未定義的數據

從 Location 中刪除類別 County General Hospital。

Location = removecats(Location,'County General Hospital');

顯示分類數組 Location 的前八個元素。

Location(1:8)

ans = 8x1 categorical

St. Mary's Medical Center

St. Mary's Medical Center

St. Mary's Medical Center

St. Mary's Medical Center

St. Mary's Medical Center

刪除 County General Hospital 類別后,先前屬于該類別的元素不再屬于為 Location 定義的任何類別。分類數組將這些元素表示為 undefined。

使用 isundefined 函數可查找被測數據中不屬于任何類別的值。

undefinedIndex = isundefined(Location);

undefinedIndex 是一個 62×1 的分類數組,其中與 Location 中所有未定義元素相對應的位置均包含邏輯值 true (1)。

設置未定義的元素

使用 summary 函數輸出 Location 中未定義的元素數量。

summary(Location)

St. Mary's Medical Center 24

38

Location 的第一個元素屬于 St. Mary's Medical Center 類別。將第一個元素設置為 undefined,使其不再屬于任何類別。

Location(1) = '';

summary(Location)

St. Mary's Medical Center 23

39

您可以將選定元素設置為 undefined 而不刪除類別或更改其他元素的類別。將元素設置為 undefined 以指示包含未知值的元素。

預分配包含未定義元素的分類數組

您可以使用未定義的元素預分配分類數組的大小,以提高性能。創建一個分類數組,其中包含僅具有已知位置的元素。

definedIndex = ~isundefined(Location);

newLocation = Location(definedIndex);

summary(newLocation)

St. Mary's Medical Center 23

擴展 newLocation 的大小,使其成為一個 200×1 的分類數組。將上一新元素設置為 undefined。所有其他新元素也會設置為 undefined。23 個原始元素將保留它們已有的值。

newLocation(200) = '';

summary(newLocation)

St. Mary's Medical Center 23

177

newLocation 可為您計劃以后存儲到數組中的值留出空間。

總結

以上是生活随笔為你收集整理的matlab访问数组的元素,使用分类数组访问数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。