Using MATLAB (779505), страница 82
Текст из файла (страница 82)
The default delimiting characters arethe set of whitespace characters. You can use the strtok function to parse asentence into words; for example,function all_words = words(input_string)remainder = input_string;all_words = '';while (any(remainder))[chopped,remainder] = strtok(remainder);all_words = strvcat(all_words,chopped);end18-1318Character Arrays (Strings)The strmatch function looks through the rows of a character array or cell arrayof strings to find strings that begin with a given series of characters. It returnsthe indices of the rows that begin with these characters.maxstrings = strvcat('max','minimax','maximum')maxstrings =maxminimaxmaximumstrmatch('max',maxstrings)ans =1318-14String/Numeric ConversionString/Numeric ConversionMATLAB’s string/numeric conversion functions change numeric values intocharacter strings. You can store numeric values as digit-by-digit stringrepresentations, or convert a value into a hexadecimal or binary string.Consider a the scalarx = 5317;By default, MATLAB stores the number x as a 1-by-1 double array containingthe value 5317.
The int2str (integer to string) function breaks this scalar intoa 1-by-4 vector containing the string '5317'.y = int2str(x);size(y)ans =14A related function, num2str, provides more control over the format of theoutput string. An optional second argument sets the number of digits in theoutput string, or specifies an actual format.p = num2str(pi,9)p =3.14159265Both int2str and num2str are handy for labeling plots. For example, thefollowing lines use num2str to prepare automated labels for the x-axis of a plot.function plotlabel(x,y)plot(x,y)str1 = num2str(min(x));str2 = num2str(max(x));out = ['Value of f from ' str1 ' to ' str2];xlabel(out);Another class of numeric/string conversion functions changes numeric valuesinto strings representing a decimal value in another base, such as binary or18-1518Character Arrays (Strings)hexadecimal representation. For example, the dec2hex function converts adecimal value into the corresponding hexadecimal string.dec_num = 4035;hex_num = dec2hex(dec_num)hex_num =FC3See the strfun directory for a complete listing of string conversion functions.Array/String ConversionThe MATLAB function mat2str changes an array to a string that MATLAB canevaluate.
This string is useful input for a function such as eval, whichevaluates input strings just as if they were typed at the MATLAB commandline.Create a 2-by-3 array A.A = [1 2 3; 4 5 6]A =142536mat2str returns a string that contains the text you would enter to create A atthe command line.B = mat2str(A)B =[1 2 3; 4 5 6]18-1619Multidimensional ArraysMultidimensional Arrays . . . . . .
.Creating Multidimensional Arrays . . . .Accessing Multidimensional Array PropertiesIndexing . . . . . . . . . . . . . . .Reshaping . . . . . . . . . . . . . .Permuting Array Dimensions . . . . . ...................................... 19-3. 19-4. 19-8. 19-919-1019-12Computing with Multidimensional ArraysOperating on Vectors .
. . . . . . . . . .Operating Element-by-Element . . . . . . .Operating on Planes and Matrices . . . . . .....................19-1419-1419-1419-15Organizing Data in Multidimensional Arrays . . . . 19-16Multidimensional Cell Arrays . . . . . . . . . . . 19-18Multidimensional Structure Arrays . . .
. . . . . 19-19Applying Functions to Multidimensional Structure Arrays . 19-2019Multidimensional ArraysThis chapter discusses multidimensional arrays, MATLAB arrays with morethan two dimensions. Multidimensional arrays can be numeric, character, cell,or structure arrays. These arrays are broadly useful—for example, in therepresentation of multivariate data, or multiple pages of two-dimensional data.This chapter covers the following topics:• “Multidimensional Arrays”• “Computing with Multidimensional Arrays”• “Organizing Data in Multidimensional Arrays”• “Multidimensional Cell Arrays”• “Multidimensional Structure Arrays”MATLAB provides a number of functions that directly supportmultidimensional arrays.
You can extend this support by creating M-files thatwork with your data architecture.19-2FunctionDescriptioncatConcatenate arrays.ipermuteInverse permute array dimensions.ndgridGenerate arrays for N-D functions and interpolation.ndimsNumber of array dimensions.permutePermute array dimensions.shiftdimShift dimensions.squeezeRemove singleton dimensions.Multidimensional ArraysMultidimensional ArraysMultidimensional arrays in MATLAB are an extension of the normaltwo-dimensional matrix. Matrices have two dimensions: the row dimensionand the column dimension.column(1,1) (1,2) (1,3) (1,4)row(2,1) (2,2) (2,3) (2,4)(3,1) (3,2) (3,3) (3,4)(4,1) (4,2) (4,3) (4,4)You can access a two-dimensional matrix element with two subscripts: the firstrepresenting the row index, and the second representing the column index.Multidimensional arrays use additional subscripts for indexing.
Athree-dimensional array, for example, uses three subscripts:• The first references array dimension 1, the row.• The second references dimension 2, the column.• The third references dimension 3. This guide uses the concept of a page torepresent dimensions 3 and higher.(1,1,3) (1,2,3) (1,3,3) (1,4,3)page(2,1,3) (2,2,3) (2,3,3) (2,4,3)(3,1,3) (3,2,3) (3,3,3) (3,4,3)(1,1,2) (1,2,2) (1,3,2) (4,1,3)(1,4,2) (4,2,3) (4,3,3) (4,4,3)(2,1,2) (2,2,2) (2,3,2) (2,4,2)column(3,1,2) (3,2,2) (3,3,2) (3,4,2)(1,1,1) (1,2,1) (1,3,1) (4,1,2)(1,4,1) (4,2,2) (4,3,2) (4,4,2)row(2,1,1) (2,2,1) (2,3,1) (2,4,1)(3,1,1) (3,2,1) (3,3,1) (3,4,1)(4,1,1) (4,2,1) (4,3,1) (4,4,1)19-319Multidimensional ArraysTo access the element in the second row, third column of page 2, for example,you use the subscripts (2,3,2).A(:,:,1) =A(2,3,2)1480–126453218393621480–12321A(:,:,2) =645839362As you add dimensions to an array, you also add subscripts.
A four-dimensionalarray, for example, has four subscripts. The first two reference a row-columnpair; the second two access the third and fourth dimensions of data.Note The general multidimensional array functions reside in the datatypesdirectory.Creating Multidimensional ArraysYou can use the same techniques to create multidimensional arrays that youuse for two-dimensional matrices. In addition, MATLAB provides a specialconcatenation function that is useful for building multidimensional arrays.This section discusses:• Generating arrays using indexing• Generating arrays using MATLAB functions• Using the cat function to build multidimensional arraysGenerating Arrays Using IndexingOne way to create a multidimensional array is to create a two-dimensionalarray and extend it.
For example, begin with a simple two-dimensional array A.19-4Multidimensional ArraysA = [5 7 8; 0 1 9; 4 3 6];A is a 3-by-3 array, that is, its row dimension is 3 and its column dimension is3. To add a third dimension to A,A(:,:,2) = [1 0 4; 3 5 6; 9 8 7]MATLAB responds withA(:,:,1) =504713896058467A(:,:,2) =139You can continue to add rows, columns, or pages to the array using similarassignment statements.Extending Multidimensional Arrays. To extend A in any dimension:• Increment or add the appropriate subscript and assign the desired values.• Assign the same number of elements to corresponding array dimensions.
Fornumeric arrays, all rows must have the same number of elements, all pagesmust have the same number of rows and columns, and so on.You can take advantage of MATLAB’s scalar expansion capabilities, togetherwith the colon operator, to fill an entire dimension with a single value.A(:,:,3) = 5;A(:,:,3)ans =55555555519-519Multidimensional ArraysTo turn A into a 3-by-3-by-3-by-2, four-dimensional array, enterA(:,:,1,2) = [1 2 3; 4 5 6; 7 8 9];A(:,:,2,2) = [9 8 7; 6 5 4; 3 2 1];A(:,:,3,2) = [1 0 1; 1 1 0; 0 1 1];Note that after the first two assignments MATLAB pads A with zeros, asneeded, to maintain the corresponding sizes of dimensions.Generating Arrays Using MATLAB FunctionsYou can use MATLAB functions such as randn, ones, and zeros to generatemultidimensional arrays in the same way you use them for two-dimensionalarrays. Each argument you supply represents the size of the correspondingdimension in the resulting array.
For example, to create a 4-by-3-by-2 array ofnormally distributed random numbers.B = randn(4,3,2)To generate an array filled with a single constant value, use the repmatfunction. repmat replicates an array (in this case, a 1-by-1 array) through avector of array dimensions.B = repmat(5,[3 4 2])B(:,:,1) =555555B(:,:,2) =555555555555555555Note Any dimension of an array can have size zero, making it a form ofempty array.















