V_NEARNONZ replace each zero element with the nearest non-zero element [V,Y,W]=v_nearnonz(X,D) Inputs: x input vector, matrix or larger array d dimension to apply filter along [default 1st non-singleton] Outputs: v v is the same size as x but with each zero entry replaced by the nearest non-zero value along dimension d elements equidistant from two non-zero entries will be taken from the higher index y y is the same size as x and gives the index along dimension d from which the corresponding entry in v was taken If there are no non-zero entries, then the corresponding elements of y will be zero. w w is the same size as x and gives the distance (+ or -) to the nearest non-zero entry in x

0001 function [v,y,w]=v_nearnonz(x,d) 0002 %V_NEARNONZ replace each zero element with the nearest non-zero element [V,Y,W]=v_nearnonz(X,D) 0003 % 0004 % Inputs: x input vector, matrix or larger array 0005 % d dimension to apply filter along [default 1st non-singleton] 0006 % 0007 % Outputs: v v is the same size as x but with each zero entry replaced by 0008 % the nearest non-zero value along dimension d 0009 % elements equidistant from two non-zero entries will be taken 0010 % from the higher index 0011 % y y is the same size as x and gives the index along dimension d 0012 % from which the corresponding entry in v was taken 0013 % If there are no non-zero entries, then the corresponding 0014 % elements of y will be zero. 0015 % w w is the same size as x and gives the distance (+ or -) to the 0016 % nearest non-zero entry in x 0017 0018 % Copyright (C) Mike Brookes 1997 0019 % Version: $Id: v_nearnonz.m 10865 2018-09-21 17:22:45Z dmb $ 0020 % 0021 % VOICEBOX is a MATLAB toolbox for speech processing. 0022 % Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html 0023 % 0024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0025 % This program is free software; you can redistribute it and/or modify 0026 % it under the terms of the GNU General Public License as published by 0027 % the Free Software Foundation; either version 2 of the License, or 0028 % (at your option) any later version. 0029 % 0030 % This program is distributed in the hope that it will be useful, 0031 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0032 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0033 % GNU General Public License for more details. 0034 % 0035 % You can obtain a copy of the GNU General Public License from 0036 % http://www.gnu.org/copyleft/gpl.html or by writing to 0037 % Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. 0038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0039 e=size(x); 0040 p=prod(e); 0041 if nargin<2 % if no dimension given, find the first non-singleton 0042 d=find(e>1,1); 0043 if ~numel(d) 0044 d=1; 0045 end 0046 end 0047 k=e(d); % size of active dimension 0048 q=p/k; % size of remainder 0049 if d==1 0050 z=reshape(x,k,q); 0051 else 0052 z=shiftdim(x,d-1); 0053 r=size(z); 0054 z=reshape(z,k,q); 0055 end 0056 xx=z~=0; 0057 cx=cumsum(xx); 0058 [i,j]=find(z); 0059 qq=cx(xx); 0060 pos=full(sparse(qq,j,i,k,q)); % list the positions of non-zero elements in each column 0061 mp=ceil((pos(1:end-1,:)+pos(2:end,:))*0.5); % find the mid point between consecutive non-zero elements 0062 [i2,j2]=find(pos(2:end,:)>0); 0063 zz=1+cumsum(full(sparse(mp(pos(2:end,:)>0),j2,1,k,q))); 0064 y=pos(zz+repmat((0:q-1)*k,k,1)); 0065 v=z(max(y,1)+repmat((0:q-1)*k,k,1)); 0066 w=y-repmat((1:k)',1,q); 0067 w(y==0)=0; 0068 if d==1 0069 y=reshape(y,e); 0070 v=reshape(v,e); 0071 w=reshape(w,e); 0072 else 0073 y=shiftdim(reshape(y,r),length(e)+1-d); 0074 v=shiftdim(reshape(v,r),length(e)+1-d); 0075 w=shiftdim(reshape(w,r),length(e)+1-d); 0076 end

Generated by