Home > FMAToolbox > General > Insert.m

Insert

PURPOSE ^

Insert - Insert lines in a matrix.

SYNOPSIS ^

function y = Insert(x,lines,where)

DESCRIPTION ^

Insert - Insert lines in a matrix.

  USAGE

    result = Insert(matrix,lines,indices)

    array          matrix where the lines should be inserted
    lines          list of values to insert
    indices        list of (possibly repeated) matrix line numbers
                   after which new lines should be inserted (use 0
                   to insert before first line)

  EXAMPLES

    >> Insert([1;2;3;4;5],[-1;-2;-3],[0;3;5])

    ans =

      -1
       1
       2
       3
      -2
       4
       5
      -3

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function y = Insert(x,lines,where)
0002 
0003 %Insert - Insert lines in a matrix.
0004 %
0005 %  USAGE
0006 %
0007 %    result = Insert(matrix,lines,indices)
0008 %
0009 %    array          matrix where the lines should be inserted
0010 %    lines          list of values to insert
0011 %    indices        list of (possibly repeated) matrix line numbers
0012 %                   after which new lines should be inserted (use 0
0013 %                   to insert before first line)
0014 %
0015 %  EXAMPLES
0016 %
0017 %    >> Insert([1;2;3;4;5],[-1;-2;-3],[0;3;5])
0018 %
0019 %    ans =
0020 %
0021 %      -1
0022 %       1
0023 %       2
0024 %       3
0025 %      -2
0026 %       4
0027 %       5
0028 %      -3
0029 
0030 
0031 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0032 %
0033 % This program is free software; you can redistribute it and/or modify
0034 % it under the terms of the GNU General Public License as published by
0035 % the Free Software Foundation; either version 3 of the License, or
0036 % (at your option) any later version.
0037 
0038 where = where(:);
0039 mx = size(x,1);
0040 nx = size(x,2);
0041 ml = length(where);
0042 if size(lines,1) == 1,
0043     lines = repmat(lines,ml,1);
0044 end
0045 
0046 % Lines in x must be moved to new indices in y
0047 w = Accumulate(where+1,1,[mx+1,1]);
0048 new = cumsum(w)+(1:mx+1)';
0049 new(end) = [];
0050 
0051 % Copy x into y
0052 y = zeros(mx+ml,nx);
0053 y(new,:) = x;
0054 
0055 inserted = where+(1:ml)';
0056 y(inserted,:) = lines;

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005