Main Content

wkeep

Keep part of vector or matrix

    Description

    Y = wkeep(X,L,opt) extracts the vector Y from the vector X. The length of Y is L.

    If opt is 'c', 'l', or 'r', Y is the central, left, or right part, respectively, of X.

    The syntax Y = wkeep(X,L) is equivalent to Y = wkeep(X,L,'c').

    Y = wkeep(X,L,first) extracts the vector X(first:first+L-1).

    Y = wkeep(X,S) extracts the central part of the matrix X. The size of Y is S.

    example

    Y = wkeep(X,S,[firstr,firstc]) extracts the submatrix of the matrix X, of size S and starting from X(firstr,firstc).

    Examples

    collapse all

    Create a vector.

    x = 1:10;

    Extract a vector of length 6 from the central part of x. Confirm both possible syntaxes return the same vector.

    y = wkeep(x,6,'c')
    y = 1×6
    
         3     4     5     6     7     8
    
    
    y = wkeep(x,6)
    y = 1×6
    
         3     4     5     6     7     8
    
    

    Extract a vector of length 7 from the central part of x.

    y = wkeep(x,7,'c')
    y = 1×7
    
         2     3     4     5     6     7     8
    
    

    Extract two vectors of length 6, one from the left part of x, and the other from the right part of x.

    y = wkeep(x,6,'l')
    y = 1×6
    
         1     2     3     4     5     6
    
    
    y = wkeep(x,6,'r')
    y = 1×6
    
         5     6     7     8     9    10
    
    

    Create a 5-by-5 matrix.

    x = magic(5)
    x = 5×5
    
        17    24     1     8    15
        23     5     7    14    16
         4     6    13    20    22
        10    12    19    21     3
        11    18    25     2     9
    
    

    Extract from the center of x a 3-by-2 matrix.

    y = wkeep(x,[3 2])
    y = 3×2
    
         5     7
         6    13
        12    19
    
    

    Extract from x the 2-by-4 submatrix starting at x(3,1).

    y = wkeep(x,[2 4],[3 1])
    y = 2×4
    
         4     6    13    20
        10    12    19    21
    
    

    Input Arguments

    collapse all

    Input, specified as a vector or matrix.

    Data Types: single | double

    Length of vector to extract from the input vector X, specified as an integer or Inf. If L is specified as Inf, wkeep returns the input vector X.

    Data Types: single | double

    Location of extraction from the input vector X, specified as:

    • 'c' — central part of the vector

    • 'l' — left part of the vector

    • 'r' — right part of the vector

    Example: wkeep(1:10,4,'r') returns the extraction [7 8 9 10].

    Starting index of the input vector X, specified as a positive integer. The first element in the extraction is X(first).

    Data Types: single | double

    Dimensions of submatrix to extract from the input matrix X, specified as a two-element vector. Each element of S is a positive integer or Inf.

    Example: If X is a 27-by-5 matrix, wkeep(X,[Inf 3]) extracts the 27-by-3 submatrix from the central part of X.

    Data Types: single | double

    Starting row, column indices of the input matrix X, specified as two positive integers. The value of the extraction Y(1,1) is X(firstr,firstc).

    Example: wkeep(X,[3 2],[1 4]) extracts a 3-by-2 submatrix from the matrix X starting from X(1,4).

    Data Types: single | double

    Extended Capabilities

    Version History

    Introduced before R2006a

    See Also