在MATLAB中查詢包含我的字串的單元格索引


在MATLAB中,單元陣列是一種用於儲存不同資料型別和大小的資料的資料結構。簡而言之,單元陣列允許我們儲存不同型別的資料,如數字、字元、字串、陣列等等。

在單元陣列中,每個單元格可以包含特定型別的資料。單元陣列是處理異構資料的有力工具。

在MATLAB中,花括號 '{}' 用於建立單元陣列,即

CellArray = {10, 'TutorialsPoint', [2 3 4]};

因此,當我們需要將不同型別的資料或不同大小的資料一起儲存時,單元陣列起著至關重要的作用。

在本篇文章中,我們的主要目標是使用MATLAB查詢單元陣列中特定字串的索引。我們可以使用以下兩種方法中的任何一種:

  • 使用迴圈和條件語句的組合

  • 使用內建函式

現在讓我們藉助示例程式詳細討論每種方法。

使用迴圈和條件語句查詢我的字串的索引

在MATLAB中,我們可以使用條件語句和迴圈來查詢包含我的字串的單元格的索引。

示例

下面的MATLAB程式演示了查詢包含我的字串的單元格索引的程式碼實現。

% MATLAB program to find index of my string using loops and conditional statements
% Create a sample cell array of strings
A = {'Tutorials', 'Point', 'is', 'a', 'Great', 'Platform', 'to', 'Learn', 'Programming'};

% Specifying my string whose index I want to know
S = 'Learn';

% Create an empty array to store the index
I = [];

% Loop through the cell array
for i = 1:numel(A)
    if strcmp(A{i}, S)
        I = [I, i];
    end
end

% Display the index of cells containing my string
disp('Index of cells containing my string:');
disp(I);

輸出

Index of cells containing my string:
     8

解釋

在這個MATLAB程式碼中,我們首先建立一個包含各種字串的單元陣列 'A'。然後,我們指定要查詢其在單元陣列中索引的字串 'S',在本例中為 'Learn',您可以根據需要更改它。

之後,我們建立一個空陣列 'I' 來儲存包含指定字串的單元格的索引。接下來,我們使用 'for' 迴圈遍歷單元陣列,其中我們使用 'strcmp' 函式將單元格字串與指定字串進行比較。如果兩個字串匹配,則包含該字串的單元格的索引將儲存到索引陣列 'I' 中。

最後,我們使用 'disp' 函式顯示包含指定字串的單元格的索引。

需要注意的是,當單元陣列具有大型資料集時,迴圈方法效率不高。在這種情況下,我們使用一些內建函式來查詢單元陣列中指定字串的索引。

現在,讓我們使用內建函式的組合來執行相同的操作。

使用內建函式查詢我的字串的索引

在MATLAB中,我們可以使用內建函式的組合來查詢包含指定字串的單元格的索引。下面將藉助示例程式解釋一些常用的內建函式組合來查詢單元格的索引。

示例

下面的MATLAB程式碼展示瞭如何使用 "find" 和 "strcmp" 函式查詢我的字串的索引

% Create a sample cell array of strings
A = {'Tutorials', 'Point', 'is', 'a', 'Great', 'Platform', 'to', 'Learn', 'Programming'};

% Specifying my string whose index I want to know
S = 'Learn';

% Find index of cell containing the specified string
I = find(strcmp(A, S));

% Display the index of cells containing my string
disp('Index of cells containing my string:');
disp(I);

輸出

Index of cells containing my string:
     8

解釋

在這個MATLAB程式碼中,我們首先建立一個包含各種字串的示例單元陣列 'A'。接下來,我們指定要查詢其在單元陣列中索引的字串 'S'。之後,我們使用 "find" 和 "strcmp" 函式的組合來查詢包含指定字串的單元格的索引,在本例中為 'Learn'。最後,我們使用 'disp' 函式顯示結果。

示例

下面的MATLAB程式展示瞭如何使用 "find" 和 "contains" 函式查詢我的字串的索引

% MATLAB program to find index of my string using 'find' and 'contains' functions
% Create a sample cell array of strings
A = {'Tutorials', 'Point', 'is', 'a', 'Great', 'Platform', 'to', 'Learn', 'Programming'};

% Specifying my string whose index I want to know
S = 'Learn';

% Find index of cell containing the specified string
I = find(contains(A, S));

% Display the index of cells containing my string
disp('Index of cells containing my string:');
disp(I);

輸出

Index of cells containing my string:
     8

解釋

這個MATLAB程式碼的實現和執行與之前的程式碼相同。唯一的區別是,在這個程式碼中,我們使用 'contains' 函式代替 'strcmp' 函式來比較指定字串與單元陣列的字串。

示例

現在,讓我們考慮另一個示例程式來查詢包含我的字串的多個單元格的索引。

% MATLAB program to find index of cells containing my strings
% Create a sample cell array of strings
A = {'Tutorials', 'Point', 'is', 'a', 'Great', 'Platform', 'to', 'Learn', 'Programming'};

% Specifying my string whose index I want to know
S1 = 'Tutorials';
S2 = 'Platform';
S3 = 'Programming';

% Find indices of cells containing the specified strings
I1 = find(strcmp(A, S1));
I2 = find(strcmp(A, S2));
I3 = find(strcmp(A, S3));

% Display the indices of cells containing my strings
disp('Index of cell containing Tutorials:');
disp(I1);
disp('Index of cell containing Platform:');
disp(I2);
disp('Index of cell containing Programming:');
disp(I3);

輸出

Index of cell containing Tutorials:
     1

Index of cell containing Platform:
     6

Index of cell containing Programming:
     9

結論

在本教程中,我們解釋瞭如何在單元陣列中查詢包含指定字串的單元格的索引的概念,並藉助示例演示了實際的程式碼實現。

更新於:2023年9月7日

87 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告