Using MATLAB (779505), страница 84
Текст из файла (страница 84)
For example, the sinfunction returns an array the same size as the function’s input argument. Eachelement of the output array is the sine of the corresponding element of theinput array.Similarly, the arithmetic, logical, and relational operators all work withcorresponding elements of multidimensional arrays that are the same size inevery dimension.
If one operand is a scalar and one an array, the operatorapplies the scalar to each element of the array.19-14Computing with Multidimensional ArraysOperating on Planes and MatricesFunctions that operate on planes or matrices, such as the linear algebra andmatrix functions in the matfun directory, do not accept multidimensionalarrays as arguments. That is, you cannot use the functions in the matfundirectory, or the array operators ∗, ^, \, or /, with multidimensional arguments.Supplying multidimensional arguments or operands in these cases results inan error.You can use indexing to apply a matrix function or operator to matrices withina multidimensional array. For example, create a three-dimensional array AA = cat(3,[1 2 3;9 8 7;4 6 5],[0 3 2;8 8 4;5 3 5],[6 4 7;6 8 5;...5 4 3]);Applying the eig function to the entire multidimensional array results in anerror.eig(A)??? Error using ==> eigInput arguments must be 2-D.You can, however, apply eig to planes within the array.
For example, use colonnotation to index just one page (in this case, the second) of the array.eig(A(:,:,2))ans =–2.626012.91292.7131Note In the first case, subscripts are not colons, you must use squeeze toavoid an error. For example, eig(A(2,:,:)) results in an error because thesize of the input is [1 3 3].
The expression eig(squeeze(A(2,:,:))),however, passes a valid two-dimensional matrix to eig.19-1519Multidimensional ArraysOrganizing Data in Multidimensional ArraysYou can use multidimensional arrays to represent data in two ways:• As planes or pages of two-dimensional data. You can then treat these pagesas matrices.• As multivariate or multidimensional data. For example, you might have afour-dimensional array where each element corresponds to either atemperature or air pressure measurement taken at one of a set of equallyspaced points in a room.For example, consider an RGB image.
For a single image, a multidimensionalarray is probably the easiest way to store and access data.Array RGB0.689 0.706 0.118 0.884 ...Page 3 – 0.535 0.532 0.653 0.925 ...blue0.314 0.265 0.159 0.101 ...intensity 0.553 0.633 0.528 0.493 ...0.112Page 1– 0.765red1.000intensity 0.4550.0211.0001.0000.9900.9020.441 0.465 0.5120.342 0.647 0.515 0.3980.816 0.401...
0.421Page 2 –0.111 0.300 0.205 0.3200.526 0.988... 0.912green0.523 0.428 0.712 0.1190.929 0.182... 0.219intensity 0.214 0.604 0.918 0.1250.344 0.495... 0.128values0.100 0.121 0.113 0.126 ... .0.986 0.234 0.2880.432 0.187... 0.204 0.175 ... .0.128 0.863 0.2080.521 0.576... 0.760 0.531 ... .0.985 0.761 0.1090.698 0.995...
0.997 0.910 ...0.783 0.224 0.4260.395 0.727... 0.995 0.726 ...0.500 0.311 0.123 ... .1.000 0.867 0.051 ... .0.945 0.998 0.893 ... .0.941 1.000 0.876 ...0.867 0.834 0.798 ......To access an entire plane of the image, usered_plane = RGB(:,:,1);19-160.5120.3980.7130.3280.133...............Organizing Data in Multidimensional ArraysTo access a subimage, usesubimage = RGB(20:40,50:85,:);The RGB image is a good example of data that needs to be accessed in planesfor operations like display or filtering.
In other instances, however, the dataitself might be multidimensional. For example, consider a set of temperaturemeasurements taken at equally spaced points in a room. Here the location ofeach value is an integral part of the data set – the physical placement inthree-space of each element is an aspect of the information. Such data alsolends itself to representation as a multidimensional array.Array TEMP67.9° 68.0° 67.9°67.8° 67.8° 67.9°67.9° 67.6°68.0° 67.5°68.0° 67.7°67.7° 67.8° 67.7°68.0° 68.0°67.7° 67.8°67.5° 67.5°67.9° 67.8° 67.6°67.8° 67.6° 67.6°Now to find the average of all the measurements, usemean(mean(mean(TEMP)));To obtain a vector of the “middle” values (element (2,2)) in the room on eachpage, useB = TEMP(2,2,:);19-1719Multidimensional ArraysMultidimensional Cell ArraysLike numeric arrays, the framework for multidimensional cell arrays inMATLAB is an extension of the two-dimensional cell array model.
You can usethe cat function to build multidimensional cell arrays, just as you use it fornumeric arrays.For example, create a simple three-dimensional cell array C.A{1,1} = [1 2;4 5];A{1,2} = 'Name';A{2,1} = 2–4i;A{2,2} = 7;B{1,1} = 'Name2';B{1,2} = 3;B{2,1} = 0:1:3;B{2,2} = [4 5]';C = cat(3,A,B);The subscripts for the cells of C look likecell 1,1,2'Name2cell 2,1,2cell 1,1,114cell 1,2,125cell 2,1,12–4i19-18'Name'cell 2,2,17[0 1 2 3]cell 1,2,23cell 2,2,245Multidimensional Structure ArraysMultidimensional Structure ArraysMultidimensional structure arrays are extensions of rectangular structurearrays.
Like other types of multidimensional arrays, you can build them usingdirect assignment or the cat function.patient(1,1,1).namepatient(1,1,1).testpatient(1,2,1).namepatient(1,2,1).testpatient(1,1,2).namepatient(1,1,2).testpatient(1,2,2).name1173.90;patient(1,2,2).test======='John Doe';patient(1,1,1).billing = 127.00;[79 75 73; 180 178 177.5; 220 210 205];'Ann Lane';patient(1,2,1).billing = 28.50;[68 70 68; 118 118 119; 172 170 169];'Al Smith';patient(1,1,2).billing = 504.70;[80 80 80; 153 153 154; 181 190 182];'Dora Jones';patient(1,2,2).billing == [73 73 75; 103 103 102; 201 198 200];patient(1,2,2)patient(1,1,2).name'Al Smith'.name'Dora Jones'.billing504.70.billing1173.90.test8015318180153190.test801541827510319875102200patient(1,2,1)patient(1,1,1)).name'John Doe'.name'Ann Lane'.billing127.00.billing28.50.test73103201791802207517821073177.5205.test68118172701181706811916919-1919Multidimensional ArraysApplying Functions to Multidimensional StructureArraysTo apply functions to multidimensional structure arrays, operate on fields andfield elements using indexing.
For example, find the sum of the columns of thetest array in patient(1,1,2).sum((patient(1,1,2).test));Similarly, add all the billing fields in the patient array.total = sum([patient.billing]);19-2020Structures and Cell ArraysStructures . . . . . . . . . . .
. .Building Structure Arrays . . . . . . .Accessing Data in Structure Arrays . . .Finding the size of Structure Arrays . . .Adding Fields to Structures . . . . . .Deleting Fields from Structures . . . . .Applying Functions and Operators . . .Writing Functions to Operate on StructuresOrganizing Data in Structure Arrays . .Nesting Structures . . . .
. . . . . ...........Cell Arrays . . . . . . . . . . . . . .Creating Cell Arrays . . . . . . . . . .Obtaining Data from Cell Arrays . . . . .Deleting Cells . . . . . . . . . . . . .Reshaping Cell Arrays . . . . . . . . .Replacing Lists of Variables with Cell ArraysApplying Functions and Operators . . .
.Organizing Data in Cell Arrays . . . . . .Nesting Cell Arrays . . . . . . . . . .Converting Between Cell and Numeric ArraysCell Arrays of Structures . . . . . . . .............................................................. 20-4. 20-5. 20-720-1020-1020-1020-1020-1120-1220-17..................................................................20-1920-2020-2320-2420-2520-2520-2720-2720-2920-3020-3120Structures and Cell ArraysStructures are collections of different kinds of data organized by named fields.Cell arrays are a special class of MATLAB array whose elements consist of cellsthat themselves contain MATLAB arrays.
Both structures and cell arraysprovide a hierarchical storage mechanism for dissimilar kinds of data. Theydiffer from each other primarily in the way they organize data. You access datain structures using named fields, while in cell arrays, data is accessed throughmatrix indexing operations.This table describes the MATLAB functions for working with structures andcell arrays..CategoryFunctionDescriptionStructure functionsdealDeal inputs to outputsfieldnamesGet structure field names.getfieldGet structure field contents.isfieldTrue if field is in structure array.isstructTrue for structures.rmfieldRemove structure field.setfieldSet structure field contents.structCreate or convert to structure array.struct2cellConvert structure array into cell array.cellCreate cell array.cell2structConvert cell array into structure array.celldispDisplay cell array contents.cellfunApply a cell function to a cell array.cellplotDisplay graphical depiction of cell array.dealDeal inputs to outputs.iscellTrue for cell array.Cell array functions20-2CategoryCell array of stringsfunctionsFunctionDescriptionnum2cellConvert numeric array into cell array.deblankRemove trailing blanks from a stringintersectSet the intersection of two vectorsismemberDetect members of a setsetdiffReturn the set difference of two vectorssetxorSet the exclusive-or of two vectorssortSort elements in ascending orderstrcatConcatentate stringsstrcmpCompare stringsstrmatchFind possible matches for a stringunionSet the union of two vectorsuniqueSet the unique elements of a vector20-320Structures and Cell ArraysStructuresStructures are MATLAB arrays with named “data containers” called fields.
Thefields of a structure can contain any kind of data. For example, one field mightcontain a text string representing a name, another might contain a scalarrepresenting a billing amount, a third might hold a matrix of medical testresults, and so on.patient.name'John Doe'.billing127.00.test791802207517821073177.5205Like standard arrays, structures are inherently array oriented.
A singlestructure is a 1-by-1 structure array, just as the value 5 is a 1-by-1 numericarray. You can build structure arrays with any valid size or shape, includingmultidimensional structure arrays.Note The examples in this section focus on two-dimensional structurearrays. For examples of higher-dimension structure arrays, see Chapter 19,“Multidimensional Arrays”The following list summarizes the contents of this section:• “Building Structure Arrays”• “Accessing Data in Structure Arrays”• “Finding the size of Structure Arrays”• “Adding Fields to Structures”• “Deleting Fields from Structures”• “Applying Functions and Operators”• “Writing Functions to Operate on Structures”20-4Structures• “Organizing Data in Structure Arrays”• “Nesting Structures”Building Structure ArraysYou can build structures in two ways:• Using assignment statements• Using the struct functionBuilding Structure Arrays Using Assignment StatementsYou can build a simple 1-by-1 structure array by assigning data to individualfields.
MATLAB automatically builds the structure as you go along. Forexample, create the 1-by-1 patient structure array shown at the beginning ofthis section.patient.name = 'John Doe';patient.billing = 127.00;patient.test = [79 75 73; 180 178 177.5; 220 210 205];Now enteringpatientat the command line results inname: 'John Doe'billing: 127test: [3x3 double]patient is an array containing a structure with three fields. To expand thestructure array, add subscripts after the structure name.patient(2).name = 'Ann Lane';patient(2).billing = 28.50;patient(2).test = [68 70 68; 118 118 119; 172 170 169];The patient structure array now has size [1 2].















