MATLAB 數(shù)組

MATLAB中所有數(shù)據(jù)類型的所有變量都是多維數(shù)組。向量是一維數(shù)組,矩陣是二維數(shù)組。

我們已經(jīng)討論了向量和矩陣。在本章中,我們將討論多維數(shù)組。但是,在此之前,讓我們討論一些特殊類型的數(shù)組。

MATLAB中的特殊數(shù)組

在本節(jié)中,我們將討論一些創(chuàng)建特殊數(shù)組的函數(shù)。對(duì)于所有這些功能,一個(gè)參數(shù)創(chuàng)建一個(gè)正方形數(shù)組,雙參數(shù)創(chuàng)建一個(gè)矩形數(shù)組。

zeros()函數(shù)創(chuàng)建一個(gè)全零的數(shù)組-

例如-

zeros(5)

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

ans =
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0

ones()函數(shù)創(chuàng)建一個(gè)全1的數(shù)組-

例如-

ones(4,3)

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

ans =
      1     1     1
      1     1     1
      1     1     1
      1     1     1

eye()函數(shù)創(chuàng)建一個(gè)單位矩陣。

例如-

eye(4)

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

ans =
      1     0     0     0
      0     1     0     0
      0     0     1     0
      0     0     0     1

rand()函數(shù)在(0,1)上創(chuàng)建一個(gè)均勻分布的隨機(jī)數(shù)數(shù)組-

例如-

rand(3, 5)

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

ans =
   0.8147    0.9134    0.2785    0.9649    0.9572
   0.9058    0.6324    0.5469    0.1576    0.4854
   0.1270    0.0975    0.9575    0.9706    0.8003

魔方

magic square是一個(gè)正方形,當(dāng)其元素按行,列或?qū)蔷€相加時(shí),會(huì)產(chǎn)生相同的和。

magic()函數(shù)創(chuàng)建一個(gè)魔術(shù)方陣。它采用一個(gè)單數(shù)參數(shù),該參數(shù)給出了正方形的大小。參數(shù)必須是大于或等于3的標(biāo)量。

magic(4)

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

ans =
   16     2     3    13
   5    11    10     8
   9     7     6    12
   4    14    15     1

多維數(shù)組

具有兩個(gè)以上維的數(shù)組在MATLAB中稱為多維數(shù)組。MATLAB中的多維數(shù)組是常規(guī)二維矩陣的擴(kuò)展。

通常,要生成多維數(shù)組,我們首先創(chuàng)建一個(gè)二維數(shù)組并將其擴(kuò)展。

例如,讓我們創(chuàng)建一個(gè)二維數(shù)組a。

a = [7 9 5; 6 1 9; 4 3 2]

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

a =
   7     9     5
   6     1     9
   4     3     2

數(shù)組a是3×3數(shù)組;我們可以添加第三維,通過(guò)提供類似的價(jià)值觀-

a(:, :, 2)= [ 1 2 3; 4 5 6; 7 8 9]

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

a =

ans(:,:,1) =

   0   0   0
   0   0   0
   0   0   0

ans(:,:,2) =

   1   2   3
   4   5   6
   7   8   9

我們也可以創(chuàng)建一個(gè)使用多維數(shù)組ones(),zeros()或rand()功能。

例如,

b = rand(4,3,2)

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

b(:,:,1) =
   0.0344    0.7952    0.6463
   0.4387    0.1869    0.7094
   0.3816    0.4898    0.7547
   0.7655    0.4456    0.2760

b(:,:,2) =
   0.6797    0.4984    0.2238
   0.6551    0.9597    0.7513
   0.1626    0.3404    0.2551
   0.1190    0.5853    0.5060

我們還可以使用cat()函數(shù)來(lái)構(gòu)建多維數(shù)組。它沿著指定的維度連接一個(gè)數(shù)組列表-

cat()函數(shù)的語(yǔ)法是-

B = cat(dim, A1, A2...)

在哪里,

  • B是創(chuàng)建的新數(shù)組

  • A1,A2,...是要串聯(lián)的數(shù)組

  • dim是連接數(shù)組所依據(jù)的維

實(shí)例

創(chuàng)建一個(gè)腳本文件并在其中鍵入以下代碼-

a = [9 8 7; 6 5 4; 3 2 1];
b = [1 2 3; 4 5 6; 7 8 9];
c = cat(3, a, b, [ 2 3 1; 4 7 8; 3 9 0])

運(yùn)行文件時(shí),它顯示-

c(:,:,1) =
      9     8     7
      6     5     4
      3     2     1
c(:,:,2) =
      1     2     3
      4     5     6
      7     8     9
c(:,:,3) =
      2     3     1
      4     7     8
      3     9     0

數(shù)組函數(shù)

MATLAB提供了以下功能來(lái)對(duì)數(shù)組內(nèi)容進(jìn)行排序,旋轉(zhuǎn),置換,整形或移位。

功能作用
length

向量長(zhǎng)度或最大數(shù)組維數(shù)

ndims數(shù)組維數(shù)
numel數(shù)組元素?cái)?shù)
size數(shù)組維數(shù)
iscolumn

確定輸入是否為列向量

isempty確定數(shù)組是否為空
ismatrix

確定輸入是否為矩陣

isrow確定輸入是否為行向量
isscalar確定輸入是否為標(biāo)量
isvector確定輸入是否為向量
blkdiag

根據(jù)輸入?yún)?shù)構(gòu)造塊對(duì)角矩陣

circshift循環(huán)移位數(shù)組
ctranspose復(fù)共軛轉(zhuǎn)置
diag對(duì)角矩陣和矩陣的對(duì)角線
flipdim

沿指定的維度翻轉(zhuǎn)數(shù)組

fliplr從左到右翻轉(zhuǎn)矩陣
flipud上下翻轉(zhuǎn)矩陣
ipermute

倒置 N-D 數(shù)組的維數(shù)

permute

重新排列N-D數(shù)組的維數(shù)

repmat復(fù)制和切片數(shù)組
reshape重塑數(shù)組
rot90將矩陣旋轉(zhuǎn)90度
shiftdim移動(dòng)維度
issorted確定集合元素是否按排序順序
sort

按升序或降序?qū)?shù)組元素排序

sortrows按升序?qū)π羞M(jìn)行排序
squeeze刪除單例維度
transpose轉(zhuǎn)置
vectorize向量化表達(dá)

實(shí)例

以下示例說(shuō)明了上面提到的一些功能。

元素的長(zhǎng)度、維數(shù)和數(shù)量-

創(chuàng)建一個(gè)腳本文件并在其中鍵入以下代碼-

x = [7.1, 3.4, 7.2, 28/4, 3.6, 17, 9.4, 8.9];
length(x)      % length of x vector
y = rand(3, 4, 5, 2);
ndims(y)       % no of dimensions in array y
s = ['Zara', 'Nuha', 'Shamim', 'Riz', 'Shadab'];
numel(s)       % no of elements in s

運(yùn)行文件時(shí),它顯示以下結(jié)果-

ans =  8
ans =  4
ans =  23

數(shù)組元素的循環(huán)移位

創(chuàng)建一個(gè)腳本文件并在其中鍵入以下代碼-

a = [1 2 3; 4 5 6; 7 8 9]  % the original array a
b = circshift(a,1)         %  circular shift first dimension values down by 1.
c = circshift(a,[1 -1])    % circular shift first dimension values % down by 1 
                           % and second dimension values to the left % by 1.

運(yùn)行文件時(shí),它顯示以下結(jié)果-

a =
   1     2     3
   4     5     6
   7     8     9

b =
   7     8     9
   1     2     3
   4     5     6

c =
   8     9     7
   2     3     1
   5     6     4

排序數(shù)組

創(chuàng)建一個(gè)腳本文件并在其中鍵入以下代碼-

v = [ 23 45 12 9 5 0 19 17]  % horizontal vector
sort(v)                      % sorting v
m = [2 6 4; 5 3 9; 2 0 1]    % two dimensional array
sort(m, 1)                   % sorting m along the row
sort(m, 2)                   % sorting m along the column

運(yùn)行文件時(shí),它顯示以下結(jié)果-

v =
   23    45    12     9     5     0    19    17
ans =
   0     5     9    12    17    19    23    45
m =
   2     6     4
   5     3     9
   2     0     1
ans =
   2     0     1
   2     3     4
   5     6     9
ans =
   2     4     6
   3     5     9
   0     1     2

單元數(shù)組

單元格數(shù)組是索引單元格的數(shù)組,其中每個(gè)單元格可以存儲(chǔ)不同維度和數(shù)據(jù)類型的數(shù)組。

cell函數(shù)用于創(chuàng)建單元格數(shù)組。單元格功能的語(yǔ)法是-

C = cell(dim)
C = cell(dim1,...,dimN)
D = cell(obj)

說(shuō)明,

  • C是單元數(shù)組;

  • dim是標(biāo)量整數(shù)或整數(shù)向量,用于指定單元格數(shù)組C的維數(shù);

  • dim1,...,dimN是指定C維數(shù)的標(biāo)量整數(shù);

  • obj是以下其中之一-

    • Java數(shù)組或?qū)ο?/p>

    • .NET類型System.String或System.Object的數(shù)組

實(shí)例

創(chuàng)建一個(gè)腳本文件并在其中鍵入以下代碼-

c = cell(2, 5);
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5}

運(yùn)行文件時(shí),它顯示以下結(jié)果-

c = 
{
   [1,1] = Red
   [2,1] =  1
   [1,2] = Blue
   [2,2] =  2
   [1,3] = Green
   [2,3] =  3
   [1,4] = Yellow
   [2,4] =  4
   [1,5] = White
   [2,5] =  5
}

訪問(wèn)單元數(shù)組中的數(shù)據(jù)

有兩種方法可以引用單元格數(shù)組的元素-

  • 將索引括在第一個(gè)方括號(hào)()中,以引用單元格集

  • 將索引括在大括號(hào){}中,以引用單個(gè)單元格中的數(shù)據(jù)

當(dāng)您將索引括在第一個(gè)括號(hào)中時(shí),它指的是單元格的集合。

圓括號(hào)中的單元格數(shù)組索引是指單元格集。

例如-

c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c(1:2,1:2)

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

ans = 
{
   [1,1] = Red
   [2,1] =  1
   [1,2] = Blue
   [2,2] =  2
}

您也可以使用大括號(hào)索引來(lái)訪問(wèn)單元格的內(nèi)容。

例如-

c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c{1, 2:4}

MATLAB將執(zhí)行上述語(yǔ)句并返回以下結(jié)果-

ans = Blue
ans = Green
ans = Yellow
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清