-
Cell Index
-
Content Index
>>A = [1 2 3;4 5 6;7 8 9]
B = ['abc','opq';'rst','tpl']
C = {'one','two'}
D = {101,B;A,'aha';NaN,C}
運行結果
A =
1 2 3
4 5 6
7 8 9
B =
abcopq
rsttpl
C =
'one' 'two'
D =
[ 101] [2x6 char]
[3x3 double] 'aha'
[ NaN] {1x2 cell}
cellplot(D)
元胞元素單元格(Cell)可以存儲其他任意數據類型的數組
- 數值數組
- 字符數組
- 元胞數組
- 空值
單元索引 ()
% 單元索引
cell_D = D(1,1)
% cell_D 為元胞數組
D(3,2)
輸出結果
cell_D =
[101]
ans =
{1x2 cell}
內容索引 {}
>>% 內容索引
num_D = C{1,1}
% num_D為數值數組
D{3,2}
輸出結果
num_C =
101
ans =
'one' 'two'
總結
運算時注意變量是基礎數組類型而不是元胞
復制從某篇文章
%% 了解元胞數組單元索引與內容索引
%% 元胞數組的索引
C = {'one', 'two', 'three';
1, 2, 3};
%% 1.Cell Indexing with Smooth Parentheses, () (操作數組本身)
% 1.
upperLeft = C(1:2,1:2);
% 2.
C(1,1:3) = {'first','second','third'}
% 3.If cells in your array contain numeric data, you can convert the cells to a numeric array using the cell2mat function:
% 將元胞數組中數值部分轉化為矩陣
numericCells = C(2,1:3)
numericVector = cell2mat(numericCells)
%% 2.Content Indexing with Curly Braces, {} (操作數組里面的內容)
% 1.
last = C{2,3}
% 2.
C{2,3} = 300
% 3.access the contents of multiple cells
C{1:2,1:2}
[r1c1, r2c1, r1c2, r2c2] = C{1:2,1:2}
% 因為每一個元胞數組的內容不一樣,所以不能將數據列在一個變量中,所以會按列的順序分開列出所以的內容
% 4.當數據類型相同時,可以使用 [] 合並數據
nums = [C{2,:}]
% * 數組可以使用 {} 合並
% 5.元胞數組的賦值操作
[r1c1, r2c1, r1c2, r2c2] = C{1:2,1:2} % 對內容賦值
rc = {C{1:2,1:2}} % 對內容操作 + 合並內容
rc = C(1:2,1:2) % 對元胞數組進行操作賦值
r1c1 = C{1:2,1:2} % 這種操作只能將第一個賦過去
% 6.結構體中產生的元胞數組
imname = dir(['C:\Users\ncf\Desktop\' '*.doc']);%讀入文件夾下的 doc ,imname 為結構體
file_all = {imname.name}; % imname.name 產生元胞數組(因為每一個文檔的名字長度不同)
%% 3.多維數的操作
% 1.
myNum = [1, 2, 3];
myCell = {'one', 'two'};
myStruct.Field1 = ones(3);
myStruct.Field2 = 5*ones(5);
C = {myNum, 100*myNum;
myCell, myStruct};
C{1,2}
C{1,1}(1,2) % 操作元胞中矩陣
C{2,1}{1,2} % 操作元胞中的元胞
C{2,2}.Field2(5,1)
C{2,1}{2,2} = {pi, eps}; % 嵌入新的值, 對C{2,1}的元胞數組操作