wolffd@0: function sub = ind2subv(siz,index) wolffd@0: %IND2SUBV Subscript vector from linear index. wolffd@0: % IND2SUBV(SIZ,IND) returns a vector of the equivalent subscript values wolffd@0: % corresponding to a single index into an array of size SIZ. wolffd@0: % If IND is a vector, then the result is a matrix, with subscript vectors wolffd@0: % as rows. wolffd@0: wolffd@0: %sub = ind2subvTest(siz, index); wolffd@0: n = length(siz); wolffd@0: if n==0, sub = index; return; end % added by KPM 17 Nov 07 wolffd@0: cum_size = cumprod(siz(:)'); wolffd@0: prev_cum_size = [1 cum_size(1:end-1)]; wolffd@0: index = index(:) - 1; wolffd@0: sub = rem(repmat(index,1,n),repmat(cum_size,length(index),1)); wolffd@0: sub = floor(sub ./ repmat(prev_cum_size,length(index),1))+1; wolffd@0: wolffd@0: % slow way wolffd@0: %for dim = n:-1:1 wolffd@0: % sub(:,dim) = floor(index/cum_size(dim))+1; wolffd@0: % index = rem(index,cum_size(dim)); wolffd@0: %end wolffd@0: