CHAPTER 12: New Features of Matlab 5.0

This chapter shows information gleaned from use of help on new features of Matlab 5.0 We hope to investigate these topics and commands and add notes about and/or incorporate into our 301 and 303 functions. The commands dealing with structures have been put into Chapter 13 and are illustrated in a few functions that may be useful in creating a new starting program for our Chemical engineering packages.

multidimensional arrays

objects, methods, class directories, etc.

other functions

1.0 multidimensional arrays

>>help cat

CAT Concatenate arrays. CAT(DIM,A,B) concatenates the arrays A and B along the dimension DIM. CAT(2,A,B) is the same as [A,B]. CAT(1,A,B) is the same as [A;B]. B = CAT(DIM,A1,A2,A3,A4,...) concatenates the input arrays A1, A2, etc. along the dimension DIM.

When used with comma separated list syntax, CAT(DIM,C{:}) or CAT(DIM,C.FIELD) is a convenient way to concatenate a cell or structure array containing numeric matrices into a single matrix.Examples:

a = magic(3); b = pascal(3); c = cat(4,a,b)

produces a 3-by-3-by-1-by-2 result and

s = {a b};
for i=1:length(s),
   siz{i} = size(s{i});
end
sizes = cat(1,siz{:})

produces a 2-by-2 array of size vectors.

>>help repmat

REPMAT Replicate and tile an array. B = REPMAT(A,M,N) replicates and tiles the matrix A to produce the M-by-N block matrix B. B = REPMAT(A,[M N]) produces the same thing. B = REPMAT(A,[M N P ...]) tiles the array A to produce a M-by-N-by-P-by-... block array. A can be N-D.

REPMAT(A,M,N) when A is a scalar is commonly used to produce an M-by-N matrix filled with A's value. This is much faster than A*ONES(M,N).

Example:

repmat(magic(2),2,3)
repmat(NaN,2,3)

See also MESHGRID.

 

>>help ndgrid

NDGRID Generation of arrays for N-D functions and interpolation. [X1,X2,X3,...] = NDGRID(x1,x2,x3,...) transforms the domain specified by vectors x1,x2,x3, etc. into arrays X1,X2,X3, etc. that can be used for the evaluation of functions of N variables and N-D interpolation. The i-th dimension of the output array Xi are copies of elements of the vector xi.

[X1,X2,...] = NDGRID(x) is the same as [X1,X2,...] = NDGRID(x,x,...).

For example, to evaluate the function x2*exp(-x1^2-x2^2-x^3) over the range -2 < x1 < 2, -2 < x2 < 2, -2 < x3 < 2,

[x1,x2,x3] = ndgrid(-2:.2:2, -2:.25:2, -2:.16:2); 
z = x2 .* exp(-x1.^2 - x2.^2 - x3.^2);
slice(x2,x1,x3,z,[-1.2 .8 2],2,[-2 -.2])

NDGRID is like MESHGRID except that the order of the first two input arguments are switched (i.e., [X1,X2,X3] = NDGRID(x1,x2,x3) produces the same result as [X2,X1,X3] = MESHGRID(x2,x1,x3)). Because of this, NDGRID is better suited to N-D problems that aren't spatially based, while MESHGRID is better suited to problems in cartesian space (2-D or 3-D).

See also MESHGRID, INTERPN.

>>help interpn

INTERPN N-D interpolation (table lookup).

VI = INTERPN(X1,X2,X3,...,V,Y1,Y2,Y3,...) interpolates to find VI, the values of the underlying N-D function V at the points in arrays Y1,Y2,Y3,etc. For an N-D V, INTERPN should be called with 2*N+1 arguments. Arrays X1,X2,X3,etc. specify the points at which the data V is given. Out of range values are returned as NaN's. Y1,Y2,Y3,etc. must be arrays of the same size or vectors. Vector arguments that are not the same size are passed through NDGRID to create the Y1,Y2,Y3,etc. arrays. INTERPN works for all N-D arrays with 2 or more dimensions.

VI = INTERPN(V,Y1,Y2,Y3,...) assumes X1=1:SIZE(V,1),X2=1:SIZE(V,2),etc.

VI = INTERPN(V,NTIMES) expands V by interleaving interpolates between every element, working recursively for NTIMES. VI = INTERPN(V) is the same as INTERPN(V,1).

VI = INTERPN(...,'method') specifies alternate methods. The default is linear interpolation. Available methods are:

'linear' - linear interpolation
'cubic' - cubic interpolation
'nearest' - nearest neighbor interpolation

INTERPN requires that X1,X2,X3,etc. be monotonic and plaid (as if they were created using NDGRID). Variable spacing is handled by mapping the given values in X1,X2,X3,etc. and Y1,Y2,Y3,etc. to an equally-spaced domain before interpolating. For faster interpolation when X1,X2,Y3,etc. are known to be equally spaced and monotonic, use the methods '*linear', '*cubic', or '*nearest'.

See also INTERP1, INTERP2, INTERP3, NDGRID.

 

>>help sum

SUM Sum of elements.

For vectors, SUM(X) is the sum of the elements of X. For matrices, SUM(X) is a row vector with the sum over each column. For N-D arrays, SUM(X) operates along the first non-singleton dimension.

SUM(X,DIM) sums along the dimension DIM.

Example: If

X = [0 1 2
     3 4 5]

then sum(X,1) is [3 5 7] and sum(X,2) is

[ 3
 12];

See also PROD, CUMSUM, DIFF.

 

>>help slice

SLICE Volumetric slice plot.

SLICE(X,Y,Z,V,Sx,Sy,Sz) draws slices along the x,y,z directions at the points in the vectors Sx,Sy,Sz. The arrays X,Y,Z define the coordinates for V and must be monotonic and 3-D plaid (as if produced by MESHGRID). The color at each point will be determined by 3-D interpolation into the volume V. V must be an M-by-N-by-P volume array.

SLICE(X,Y,Z,V,XI,YI,ZI) 

draws slices through the volume V along the surface defined by the arrays XI,YI,ZI.

SLICE(V,Sx,Sy,Sz)

or

SLICE(V,XI,YI,ZI) 

assumes X=1:N, Y=1:M, Z=1:P.

SLICE(...,'method') 

specifies the interpolation method to use. 'method' can be 'linear', 'cubic', or 'nearest'. 'linear' is the default (see INTERP3).

H = SLICE(...) 

returns a vector of handles to SURFACE objects.

Example: To visualize the function x*exp(-x^2-y^2-z^2) over the range -2 < x < 2, -2 < y < 2, -2 < z < 2,

[x,y,z] = meshgrid(-2:.2:2, -2:.25:2, -2:.16:2);
v = x .* exp(-x.^2 - y.^2 - z.^2);
slice(x,y,z,v,[-1.2 .8 2],2,[-2 -.2])

See also MESHGRID, INTERP3.

2.0 objects, methods and class directories, etc.

Class has help on-line. Objects and methods are described in the helpdesk.

>>help class

CLASS Create object or return object class. CLASS(OBJ) returns the class of the object OBJ. Possibilities are:

double -- double precision floating point number array

(this is the traditional MATLAB matrix or array)

sparse -- 2-D real (or complex) sparse matrix
struct -- Structure array
cell -- cell array
char -- Character array
<class_name> -- Custom object class

Within a constructor method, CLASS(S,'class_name') creates an object of class 'class_name' from the structure S. This syntax is only valid in a function named <class_name>.m in a directory named @<class_name> (where <class_name> is the same as the string passed into CLASS). On VMS, the method directory has the name #<class_name>.

CLASS(S,'class_name',PARENT1,PARENT2,...) also inherits the methods and fields of the parent objects PARENT1, PARENT2, ...

See also ISA, SUPERIORTO, INFERIORTO, STRUCT.

 

>>help switch

SWITCH Switch among several cases based on expression. The general form of the SWITCH statement is:

SWITCH switch_expr
CASE case_expr,

statement, ..., statement

CASE {case_expr1, case_expr2, case_expr3,...}

statement, ..., statement

...

OTHERWISE,

statement, ..., statement

END

The first CASE where the switch_expr matches the case_expr is executed. When the case expression is a cell array (as in the second case above), the case_expr matches if any of the elements of the cell array match the switch expression. If none of the case expressions match the switch expression then the OTHERWISE case is executed (if it exists). Only one CASE is executed and execution resumes with the statement after the END.

The switch_expr can be a scalar or a string. A scalar switch_expr matches a case_expr if switch_expr==case_expr. A string switch_expr matches a case_expr if strcmp(switch_expr,case_expr) returns 1 (true).

Example (assuming METHOD exists as a string variable):

switch lower(METHOD)
   case {'linear','bilinear'}, disp('Method is linear')
   case 'cubic', disp('Method is cubic')
   case 'nearest', disp('Method is nearest')
otherwise, disp('Unknown method.')
end

See also CASE, OTHERWISE, IF, WHILE, FOR, END.

>>help case

CASE SWITCH statement case.

CASE is part of the SWITCH statement syntax, whose general form is:

SWITCH switch_expr
CASE case_expr,

statement, ..., statement

CASE {case_expr1, case_expr2, case_expr3,...}

statement, ..., statement

...

OTHERWISE,

statement, ..., statement

END

See SWITCH for more details.

3.0 Other functions

>>help isequal

ISEQUAL True if arrays are identical. ISEQUAL(A,B) is 1 if the two arrays are the same size with the same contents and 0 otherwise.

ISEQUAL(A,B,C,...) is 1 if all the input arguments are equal.

See also EQ.

>>help logical

LOGICAL Convert numeric values to logical.

LOGICAL(X) returns an array that can be used for logical indexing or logical tests. Logical arrays are also created by the relational operators (==,<,>,~, etc.) and functions like ANY, ALL, ISNAN, ISINF, and ISFINITE.

A(B), where B is a logical array, returns the values of A at the indices where the real part of B is nonzero (B must be the same size as A).

Most arithmetic operations remove the logicalness from an array. Seldom necessary, the easiest method is add zero (i.e. A+0).

See also ISLOGICAL, RELOP, OPS.

>>help islogical

ISLOGICAL True for logical array.

ISLOGICAL(X) returns 1 if X is a logical array and 0 otherwise.

Logical arrays must be used to perform logical 0-1 indexing.

See also LOGICAL.

>>help relop

Relational operators.

  < > Relational operators.

The six relational operators are <, <=, >, >=, ==, and ~=. A < B does element by element comparisons between A and B and returns a matrix of the same size with elements set to one where the relation is true and elements set to zero where it is not. A and B must have the same dimensions unless one is a scalar. A scalar can be compared with anything.

   &   Logical AND.

A & B is a matrix whose elements are 1's where both A and B have non-zero elements, and 0's where either has a zero element. A and B must have the same dimensions unless one is a scalar. A scalar can be operate with anything.

  |   Logical OR.

A | B is a matrix whose elements are 1's where either A or B has a non-zero element, and 0's where both have zero elements. A and B must have the same dimensions unless one is a scalar. A scalar can be operate with anything.

   ~   Logical complement (NOT).

~A is a matrix whose elements are 1's where A has zero elements, and 0's where A has non-zero elements.

   xor Exclusive OR.

xor(A,B) is 1 where either A or B, but not both, is non-zero.

>>help bar3

BAR3 3-D bar graph.

BAR3(Y,Z) draws the columns of the M-by-N matrix Z as vertical 3-D bars. The vector Y must be monotonically increasing or decreasing.

BAR3(Z) 

uses the default value of Y=1:M. For vector inputs,

BAR3(Y,Z) 

or

BAR3(Z) 

draws LENGTH(Z) bars. The colors are set by the colormap.

 

BAR3(Y,Z,WIDTH) 

or

BAR3(Z,WIDTH) 

specifies the width of the bars. Values of WIDTH > 1, produce overlapped bars. The default value is WIDTH=0.8

BAR3(...,'detached') 

produces the default detached bar chart.

BAR3(...,'grouped')

produces a grouped bar chart.

BAR3(...,'stacked') 

produces a stacked bar chart.

BAR3(...,LINESPEC) 

uses the line color specified (one of 'rgbymckw').

H = BAR3(...) returns a vector of surface handles.

Example:

subplot(1,2,1), bar3(peaks(5))
subplot(1,2,2), bar3(rand(5),'stacked')

See also BAR, BARH, and BAR3H.

 

>>help barh

BARH Horizontal bar graph.

BARH(X,Y) draws the columns of the M-by-N matrix Y as M groups of N horizontal bars. The vector X must be monotonically increasing or decreasing.

BARH(Y) 

uses the default value of X=1:M. For vector inputs,

BARH(X,Y) 

or

BARH(Y) 

draws LENGTH(Y) bars. The colors are set by the colormap.

BARH(X,Y,WIDTH)

or

BARH(Y,WIDTH)

specifies the width of the bars. Values of WIDTH > 1, produce overlapped bars. The default value is WIDTH=0.8.

BARH(...,'grouped') 

produces the default vertical grouped bar chart.

BARH(...,'stacked') 

produces a vertical stacked bar chart.

BARH(...,LINESPEC) 

uses the line color specified (one of 'rgbymckw').

H = BARH(...) 

returns a vector of patch handles.

Examples:

subplot(3,1,1), barh(rand(10,5),'stacked'), colormap(cool)
subplot(3,1,2), barh(0:.25:1,rand(5),1)
subplot(3,1,3), barh(rand(2,3),.75,'grouped')

See also PLOT, BAR, BAR3H.

 

>>help pie3

PIE3 3-D pie chart.

PIE3(X) draws a 3-D pie plot of the data in the vector X. The values in X are normalized via X/SUM(X) to determine the area of each slice of pie. If SUM(X) <= 1.0, the values in X directly specify the area of the pie slices. Only a partial pie will be drawn if SUM(X) < 1.

H = PIE3(X,EXPLODE) 

is used to specify slices that should be pulled out from the pie. The vector EXPLODE must be the same size as X. The slices where EXPLODE is non-zero will be pulled out.

H = PIE3(...) 

returns a vector containing patch, surface, and text handles.


Continue on to Chapter 13 
Return to Table of Contents