Main Content

contourc

Low-level contour matrix computation

    Description

    M = contourc(Z) calculates a contour matrix M for a surface. Specify Z as a matrix containing the height values of the surface with respect to the x-y plane. MATLAB® determines the number of contour lines and the corresponding values. The column and row indices of Z are the x- and y-coordinates in the plane, respectively.

    Note

    The matrix returned by contourc might be inconsistent with the results from the contour, contourf, and contour3 functions. To get the matrix used by one of those functions, call that function with an output argument.

    example

    M = contourc(x,y,Z) specifies the x- and y-coordinates for the values in Z.

    example

    M = contourc(___,levels) specifies the contour lines to calculate as the last argument in any of the previous syntaxes. Specify levels as a scalar value n to calculate the contour lines at n automatically chosen levels (heights). To calculate the contour lines at specific heights, specify levels as a vector of monotonically increasing values. To calculate the contours at one height k, specify levels as a two-element row vector [k k].

    example

    Examples

    collapse all

    Create vectors x and y. Then create matrices Xm and Ym, that define a grid in the xy-plane. Define matrix Z as the height values for a paraboloid.

    x = -5:0.5:5;
    y = -5:0.5:5;
    [Xm,Ym] = meshgrid(x,y);
    Z = Xm.^2 + Ym.^2;

    Calculate the contour matrix for Z without specifying the x- and y-coordinates for Z. Return the result as matrix M1 and display the first five columns. The values in the first column indicate that the first contour line is at Z=5 with 29 vertices. The first vertex is at x=7, y=13.

    M1 = contourc(Z);
    M1(:,1:5)
    ans = 2×5
    
        5.0000    7.0000    7.7143    8.0000    9.0000
       29.0000   13.0000   14.0000   14.2857   15.0000
    
    

    Calculate the contour matrix for Z again. This time, specify the x- and y-coordinates. Display the first five columns of M2. Notice that the first Z value and the number of vertices for the first contour line are the same as in M1, but the vertex coordinates are different.

    M2 = contourc(x,y,Z);
    M2(:,1:5)
    ans = 2×5
    
        5.0000   -2.0000   -1.6429   -1.5000   -1.0000
       29.0000    1.0000    1.5000    1.6429    2.0000
    
    

    Create vectors x and y. Then create matrices Xm and Ym, that define a grid in the xy-plane. Define matrix Z as the height values for a paraboloid.

    x = -5:0.5:5;
    y = -5:0.5:5;
    [Xm,Ym] = meshgrid(x,y);
    Z = Xm.^2 + Ym.^2;

    Calculate the contour matrix at 10 levels selected by the contourc function. Return the result as matrix M and display the first five columns. The values in the first column indicate that the first contour line is at Z=4.55 with 37 vertices. The first vertex is at x=-2, y=0.697.

    M = contourc(x,y,Z,10);
    M(:,1:5)
    ans = 2×5
    
        4.5455   -2.0000   -1.8701   -1.5130   -1.5000
       37.0000    0.6970    1.0000    1.5000    1.5130
    
    

    Create vectors x and y. Then create matrices Xm and Ym, that define a grid in the xy-plane. Define matrix Z as the height values for a paraboloid.

    x = -5:0.5:5;
    y = -5:0.5:5;
    [Xm,Ym] = meshgrid(x,y);
    Z = Xm.^2 + Ym.^2;

    Calculate the contour matrix at four specific levels: 5, 10, 15, and 20. Return the result as matrix M and display the first five columns. The values in the first column indicate that the first contour line is at Z=5 with 29 vertices. The first vertex is at x=-2, y=1.

    M = contourc(x,y,Z,[5 10 15 20]);
    M(:,1:5)
    ans = 2×5
    
        5.0000   -2.0000   -1.6429   -1.5000   -1.0000
       29.0000    1.0000    1.5000    1.6429    2.0000
    
    

    Input Arguments

    collapse all

    x-coordinates, specified as a vector with length n, where [m,n] = size(Z). The default value of x is the vector (1:n). The values in the vector must be strictly increasing or decreasing.

    Example: x = 1:10

    Example: x = [1 2 3 4 5]

    Note

    contourc calculates contours using a regularly spaced contour grid, and then it transforms the data to x or y. If x or y is irregularly spaced, the shapes of the contour lines might be distorted.

    Data Types: double

    y-coordinates, specified as a vector with length m, where [m,n] = size(Z). The default value of y is the vector (1:m). The values in the vector must be strictly increasing or decreasing.

    Example: y = 1:10

    Example: y = [1 2 3 4 5]

    Note

    contourc calculates contours using a regularly spaced contour grid, and then it transforms the data to x or y. If x or y is irregularly spaced, the shapes of the contour lines might be distorted.

    Data Types: double

    z-coordinates, specified as a matrix. This matrix must have at least two rows and two columns, and it must contain at least two different values.

    Example: Z = peaks(20)

    Data Types: double

    Contour levels, specified as a scalar whole number or a vector. Use this argument to control the number and location of the contour lines. When you do not specify the levels, the contourc function chooses the levels automatically.

    • To calculate contour lines at n automatically chosen heights, specify levels as the scalar value n.

    • To calculate the contour lines at specific heights, specify levels as a vector of monotonically increasing values.

    • To calculate contour lines at a single height k, specify levels as a two-element row vector [k k].

    Example: contourc(peaks,10) calculates contour lines at 10 automatically chosen heights on the peaks function.

    Example: contourc(peaks,[-4 0 4]) calculates contour lines at 3 specific heights on the peaks function: -4, 0, and 4.

    Example: contourc(peaks,[3 3]) calculates contour lines where the height of the peaks function is 3.

    Data Types: double

    Output Arguments

    collapse all

    Contour matrix, returned as a two-row matrix of following form.

    Z1, x1,1, x1,2, ..., x1,N1, Z2, x2,1, x2,2, ..., x2,N2, Z3, ...
    N1, y1,1, y1,2, ..., y1,N1, N2, y2,1, y2,2, ..., y2,N2, N3, ...

    The columns of the matrix define the contour lines. Each contour line starts with a column containing Z and N values:

    • Zi — The height of the ith contour line

    • Ni — The number of vertices in the ith contour line

    • (xij, yij) — The coordinates of the vertices for the ith contour line, where j ranges from 1 to Ni

    Extended Capabilities

    Version History

    Introduced before R2006a