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