Home > NoiseTools > nt_vecadd.m

# nt_vecadd

## PURPOSE

y=nt_vecadd(x,v) - add vector to all rows or columns of matrix

## SYNOPSIS

function x=nt_vecadd(x,v)

## DESCRIPTION

    y=nt_vecadd(x,v) - add vector to all rows or columns of matrix

     See vecmult, bsxfun

     NoiseTools

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_fold y=fold(x,epochsize) - fold 2D to 3D
- nt_unfold y=nt_fold(x) - unfold 3D to 2D

This function is called by:

- nt_demean [y,mn]=nt_demean(x,w) - remove weighted mean over cols
- nt_multishift z=nt_multishift(x,shifts,amplitudes) - apply multiple shifts to matrix

## SOURCE CODE

    0001 function x=nt_vecadd(x,v)
    0002 %y=nt_vecadd(x,v) - add vector to all rows or columns of matrix
    0003 %
    0004 % See vecmult, bsxfun
    0005 %
    0006 % NoiseTools
    0007
    0008 % check once and for all to save time
    0009 persistent bsxfun_exists;
    0010 if isempty(bsxfun_exists);
    0011     bsxfun_exists=(exist('bsxfun')==5);
    0012     if ~bsxfun_exists;
    0013         warning('bsxfun not found.  Using repmat');
    0014     end
    0015 end
    0016
    0017
    0018 [m,n,o]=size(x);
    0019 x=nt_unfold(x);
    0020 v=nt_unfold(v);
    0021
    0022 [mm,nn]=size(x);
    0023 if numel(v)==1;
    0024     x=x+v;
    0025 elseif size(v,1)==1
    0026     if size(v,2)~=nn; error('V should have same number of columns as X'); end
    0027     if bsxfun_exists;
    0028         x=bsxfun(@plus,x,v);
    0029 %        vecop_core(x, v, 2, 1);  % 1 is the opcode of addition in vecop_core
    0030     else
    0031         x=x + repmat(v,mm,1);
    0032     end
    0033 elseif size(v,2)==1
    0034     if size(v,1)~=mm; error('V should have same number of rows as X'); end
    0035     if bsxfun_exists;
    0036         x=bsxfun(@plus,x,v);
    0037  %       y=vecop_core(x, v, 1, 1);  % 1 is the opcode of addition in vecop_core
    0038     else
    0039         x=x + repmat(v,1,nn);
    0040     end
    0041 end
    0042
    0043 x=nt_fold(x,m);