View Raw SPL
/*****************************************************************************
*                                                                            *
*   COPYARRAY.SPL Copyright (C) 2009 DSP Development Corporation            *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Copies one array into another                               *
*                                                                            *
*   Revisions:    5 Feb 2009  RRR                                            *
*                                                                            *
*****************************************************************************/

#include 

#if @HELP_COPYARRAY

    COPYARRAY

    Purpose: Copies a sub-array into another array

    Syntax:  COPYARRAY(a, b, row, col)

                a - An array, the destination array.

                b - An array, the sub-array to copy.

              row - An optional integer, the destination row.
                    Defaults to 1.

              col - An optional integer, the destination column.
                    Defaults to 1.

    Returns:
             An array, the result of copying b into a at the specified
             row and column.

    Example:
             W1: rand(640, 480);setplottype(3);
             W2: rescale(integ(randn(100, 100)), min(w1), max(w1));setplottype(3)
             W3: copyarray(W1, W2, 80, 100)

             W1 contains a 640 x 480 random array. W2 contains a 100x100
             random array. W3 contains an array that is the result of
             copying W2 into W1 at row 80, column 100;

    Remarks:
             The size of the sub-array must be equal to or smaller than
             the target array.

             Arrays are displayed in "series" order such that each column
             of the array plots from left to right and each row plots
             from bottom to top. Use TRANSPOSE to swap rows and columns.

             Use FLIPLR to reverse rows such that original starting
             rows plot from top. Also, SETY can reverse row plotting
             without effecting the actual data. For example:

                sety(w4, 480, 0)

             sets the range of W4 in the example such that rows plot from
             top to bottom.

                setyauto(w4, 480, 0)

             sets the range when the window is auto-scaled.

  See Also:
             FLIPLR
             INSERT
             SETX
             SETXAUTO
             SETY
             SETYAUTO
             TRANSPOSE

#endif


/* copy a sub-array */
copyarray(a, b, row, col)
{
        local a_nr, a_nc, b_nr, b_nc;
        local endrow, endcol, c;

        if (argc < 4)
        {
                if (argc < 3)
                {
                        if (argc < 2) error("copyarray - two input arrays required");
                        
                        row = 1;
                }
                
                col = 1;
        }

        /* array size */
        (a_nr, a_nc) = size(a);
        (b_nr, b_nc) = size(b);

        /* sub-array must be smaller */
        if (a_nr < b_nr) error("copyarray - sub-array has too many rows");
        if (a_nc < b_nc) error("copyarray - sub-array has too many columns");

        /* ending row and col */
        endrow = row + b_nr - 1;
        endcol = col + b_nc - 1;

        /* check destination extents */
        if (endrow > a_nr) error(sprintf("copyarray - start row must be <= %d",  a_nr - b_nr + 1));
        if (endcol > a_nc) error(sprintf("copyarray - start col must be <= %d",  a_nc - b_nc + 1));

        /* make a copy */
        c = a;

        /* perform the copy */
        c[row..endrow, col..endcol] = b;

        return(c);
}