Using MATLAB (779505), страница 86
Текст из файла (страница 86)
. .B(1,3)0.9860.112B(2,2). . .B(2,3).r0.765.r0.128.r0.863.g0.111.g0.300.g0.205.b0.535.b0.532.b0.653......128-by-128 structure array where each field is a single data elementPlane OrganizationIn case 1 above, each field of the structure is an entire plane of the image. Youcan create this structure usingA.r = RED;A.g = GREEN;A.b = BLUE;This approach allows you to easily extract entire image planes for display,filtering, or other tasks that work on the entire image at once.
To access theentire red plane, for example, usered_plane = A.r;Plane organization has the additional advantage of being extensible tomultiple images in this case. If you have a number of images, you can storethem as A(2), A(3), and so on, each containing an entire image.20-14StructuresThe disadvantage of plane organization is evident when you need to accesssubsets of the planes. To access a subimage, for example, you need to accesseach field separately.red_sub = A.r(2:12,13:30);grn_sub = A.g(2:12,13:30);blue_sub = A.b(2:12,13:30);Element-by-Element OrganizationCase 2 has the advantage of allowing easy access to subsets of data.
To set upthe data in this organization, usefor m = 1:size(RED,1)for n = 1:size(RED,2)B(m,n).r = RED(m,n);B(m,n).g = GREEN(m,n);B(m,n).b = BLUE(m,n);endendWith element-by-element organization, you can access a subset of data with asingle statement.Bsub = B(1:10,1:10);To access an entire plane of the image using the element-by-element method,however, requires a loop.red_plane = zeros(128,128);for k = 1:(128∗128)red_plane(k) = B(k).r;endElement-by-element organization is not the best structure array choice formost image processing applications; however, it can be the best for otherapplications wherein you will routinely need to access corresponding subsets ofstructure fields. The example in the following section demonstrates this type ofapplication.20-1520Structures and Cell ArraysExample - A Simple DatabaseConsider organizing a simple database.¹ Plane organizationA.name.address.amountB² Element-by-element organization'Ann Jones' ...'Dan Smith' ...'Kim Lee ' ......B(1)B(2)B(3).
. ..name'Ann Jones'.address'80 Park St.'.address'5 Lake Ave.'.address'116 Elm St.'.amount12.50.amount81.29.amount30.00.name'Dan Smith'.name'Kim Lee''80 Park St.' ...'5 Lake Ave.' ...'116 Elm St.' .....12.50 ...81.29 ...30.00 ......B(1).name = 'Ann Jones';B(1).address = '80 Park St.';B(1).amount = 12.5;B(2).name = 'Dan Smith';A.name = strvcat('Ann Jones','Dan Smith',...);B(2).address = '5 Lake Ave.';A.address = strvcat('80 Park St.','5 Lake Ave.',...);B(2).amount = 81.29;A.amount = [12.5;81.29;30; ...];...Each of the possible organizations has advantages depending on how you wantto access the data:• Plane organization makes it easier to operate on all field values at once.
Forexample, to find the average of all the values in the amount field,– Using plane organizationavg = mean(A.amount);– Using element-by-element organizationavg = mean([B.amount]);20-16Structures• Element-by-element organization makes it easier to access all the information related to a single client. Consider an M-file, client.m, which displaysthe name and address of a given client on screen.Using plane organization, pass individual fields.function client(name,address)disp(name)disp(address)To call the client function,client(A.name(2,:),A.address(2,:))Using element-by-element organization, pass an entire structure.function client(B)disp(B)To call the client function,client(B(2))• Element-by-element organization makes it easier to expand the string arrayfields.
If you do not know the maximum string length ahead of time for planeorganization, you may need to frequently recreate the name or address fieldto accommodate longer strings.Typically, your data does not dictate the organization scheme you choose.Rather, you must consider how you want to access and operate on the data.Nesting StructuresA structure field can contain another structure, or even an array of structures.Once you have created a structure, you can use the struct function or directassignment statements to nest structures within existing structure fields.Building Nested Structures with the struct FunctionTo build nested structures, you can nest calls to the struct function. Forexample, create a 1-by-1 structure array.A = struct('data',[3 4 7; 8 0 1],'nest',...struct('testnum','Test 1', 'xdata',[4 2 8],...'ydata',[7 1 6]));20-1720Structures and Cell ArraysYou can build nested structure arrays using direct assignment statements.These statements add a second element to the array.A(2).data = [9 3 2; 7 6 5];A(2).nest.testnum = 'Test 2';A(2).nest.xdata = [3 4 2];A(2).nest.ydata = [5 0 9];AA(1)A(2).data3 4 78 0 1.nest.testnum'Test 1'.xdata.ydata.data9 3 27 6 5.testnum'Test 2'[4 2 8].xdata[3 4 2][7 1 6].ydata[5 0 9].nestIndexing Nested StructuresTo index nested structures, append nested field names using dot notation.
Thefirst text string in the indexing expression identifies the structure array, andsubsequent expressions access field names that contain other structures.For example, the array A created earlier has three levels of nesting:• To access the nested structure inside A(1), use A(1).nest.• To access the xdata field in the nested structure in A(2), useA(2).nest.xdata.• To access element 2 of the ydata field in A(1), use A(1).nest.ydata(2).20-18Cell ArraysCell ArraysA cell array is a MATLAB array for which the elements are cells, containersthat can hold other MATLAB arrays. For example, one cell of a cell array mightcontain a real matrix, another an array of text strings, and another a vector ofcomplex values.cell 1,1cell 1,2cell 1,3'Anne Smith'398475261cell 2,1[1.43 2.985.67]'9/12/94''Class II''Obs.
1''Obs. 2'cell 2,27852.25+3i8–16i34+5i 7+.92icell 2,3231614453'text'[4 2 7]4125.02 + 8iYou can build cell arrays of any valid size or shape, including multidimensionalstructure arrays.Note The examples in this section focus on two-dimensional cell arrays. Forexamples of higher-dimension cell arrays, see Chapter 19, “MultidimensionalArrays”.The following list summarizes the contents of this section:• “Creating Cell Arrays”• “Obtaining Data from Cell Arrays”• “Deleting Cells”20-1920Structures and Cell Arrays• “Reshaping Cell Arrays”• “Replacing Lists of Variables with Cell Arrays”• “Applying Functions and Operators”• “Organizing Data in Cell Arrays”• “Nesting Cell Arrays”• “Converting Between Cell and Numeric Arrays”• “Cell Arrays of Structures”Creating Cell ArraysYou can create cell arrays by:• Using assignment statements• Preallocating the array using the cell function, then assigning data to cellsUsing Assignment StatementsYou can build a cell array by assigning data to individual cells, one cell at atime.
MATLAB automatically builds the array as you go along. There are twoways to assign data to cells:• Cell indexingEnclose the cell subscripts in parentheses using standard array notation.Enclose the cell contents on the right side of the assignment statement incurly braces, “{}.” For example, create a 2-by-2 cell array A.A(1,1)A(1,2)A(2,1)A(2,2)===={[1 4 3; 0 5 8; 7 2 9]};{'Anne Smith'};{3+7i};{–pi:pi/10:pi};Note The notation “{}” denotes the empty cell array, just as “[]” denotes theempty matrix for numeric arrays. You can use the empty cell array in any cellarray assignments.20-20Cell Arrays• Content indexingEnclose the cell subscripts in curly braces using standard array notation.Specify the cell contents on the right side of the assignment statement.A{1,1}A{1,2}A{2,1}A{2,2}====[1 4 3; 0 5 8; 7 2 9];'Anne Smith';3+7i;–pi:pi/10:pi;The various examples in this guide do not use one syntax throughout, butattempt to show representative usage of cell and content addressing.
You canuse the two forms interchangeably.Note If you already have a numeric array of a given name, don’t try to createa cell array of the same name by assignment without first clearing thenumeric array. If you do not clear the numeric array, MATLAB assumes thatyou are trying to “mix” cell and numeric syntaxes, and generates an error.Similarly, MATLAB does not clear a cell array when you make a singleassignment to it.















