matlab每日練習 lenght函式

zengweion發表於2012-12-10

matlab每日練習 lenght函式

水平有限,譯文中不妥之處懇請指正

length

Length of vector or largest array dimension

向量長度或陣列的最大維數

Syntax

函式原型

numberOfElements = length(array)

Description

函式描敘

numberOfElements = length(array) finds the number of elements along the largest dimension of an array. array is an array of any MATLAB data type and any valid dimensions. numberOfElements is a whole number of the MATLAB double class.

For nonempty arrays, numberOfElements is equivalent to max(size(array)). For empty arrays, numberOfElements is zero.

{(譯文)numberOfElements = length(array)獲取陣列最大的維數.陣列可以是任意MATLAB資料型別且具備有效的維數.返回值numberOfElements是一個MATLAB雙精度型別的整數.

對於一個非空陣列,numberOfElements等於函式max(size(array))返回值.對於空的陣列,numberOfElements的值為零.}

Examples.

 

例項

Create a 1-by-8 array X and use length to find the number of elements in the second (largest) dimension:

建立一個1乘8的陣列X,並呼叫length獲取陣列的列數目:

X = [5, 3.4, 72, 28/4, 3.61, 17 94 89];

length(X)
ans =
     8
 

Create a 4-dimensional array Y in which the third dimension is the largest. Use length to find the number of elements in that dimension:

Y = rand(2, 5, 17, 13);

length(Y)
ans =
    17
 

Create a struct array S with character and numeric fields of different lengths. Use the structfun function to apply length to each field of S:

S = struct('f1', 'Name:', 'f2', 'Charlie', ...
           'f3', 'DOB:', 'f4', 1917)
S = 
    f1: 'Name:'
    f2: 'Charlie'
    f3: 'DOB:'
    f4: 1917

structfun(@(field)length(field), S)
ans =
     5
     7
     4
     1

相關文章