>
> Yeah, I can't come up with anything of the top of my head. But I'll make
> you a deal. If you write an m-file version using for-loops, that handle
> the N-dimensional case, then I'll re-implement it in C++.

I am including here an m-version, tested, and also attaching it.  But,
really, I stand by my last post.  Why re-implement the wheel?  We
should simply use the exact same logic and facilities used by
tmpcumsum.

Nonetheless, here is a for loop that handles the n-dimensional case.
I am attaching the 3 files as well.  You can also see them here:

  http://www.gnufans.net/~deego/pub/octave/mine/dev/natrixindexcomposemy.m
  http://www.gnufans.net/~deego/pub/octave/mine/dev/natrixindexdecomposemy.m

---
The only for loop here really is the one used in tmpcummax.  The other
for loops - it goes only from 1 to the number of dimensions --- should
not really be regarded as a loop. The number of dimensions should
usually be small, and at most, they can be O(log(n)), assuming at
least two entries per dimension..
----

function mout=tmpcummax(mat,dim);
  if nargin<2;
    dim=1;
  endif

  
  ## Index logic.
  sz=szrem=size(mat);
  nsz=length(sz);
  ndim=sz(dim);
  szrem(dim)=[];
  nrest=prod(szrem);
  n=prod(sz);
  fullindices=indices1= ((1:ndim)'*ones(1,nrest))(:);
  indicesrest=natrixindexdecomposemy((ones(ndim,1)*(1:nrest))(:),szrem);
  if (dim>1);
    fullindices=[indicesrest(:,1:(dim-1)) fullindices];
  endif
  if dim<nsz;
    fullindices=[fullindices indicesrest(:,dim:end)];
  endif
  fullindices=reshape(natrixindexcomposemy(fullindices,sz),[ndim nrest]);

  ## Now, we have the indices, now we just need to do our simple loop.
  
  mout=mat;
  for ii=2:rows(fullindices);
    
mout(fullindices(ii,:))=max(mout(fullindices(ii,:)),mout(fullindices(ii-1,:)));
  endfor

endfunction




function iout=natrixindexdecomposemy(ind,sz);
  ## natrix is a general dimensional matrix with arbitrary dimensions
  ## and size sz. 
  ## Ind may be a number, or more generally be a column vector.
  ## Anyhow, assume that ind is a single number which indices the matrix(:).
  ## IOUT is then the vector of the individual components corresponding
  ## to the index.
  ## For example:
  ## For a 2x2 matrix, ind=2 maps to iout=[2 1], and ind=3 to iout=[1
  ## 2].
  ##  For example, try natrixindicesdecomposemy(1:12,[2 2 3]) to see an
  ##  interesting numbers table. 

  ind=ind(:);
  
  l=length(sz);
  iout=zeros(length(ind),length(sz));

  ir=ind-1; ## index remaining.
  for ii=1:l;
    iout(:,ii)=1+mod(ir,sz(ii));
    ir=floor(ir/sz(ii));
  endfor
endfunction



  

function iout=natrixindexcomposemy(indin,sz);
  ## Opposite of natrixindexdecomposemy
  ## Indin is a row vector. 1xK of the k indices of the matrix which has
  ## a size sz. We return iout: the corresponding single index composed
  ## from the k indices.
  ##
  ## If indin has multiple rows (R), we return the corresponding iouts
  ## for each row.

  indin=indin-1;
  
  iout=zeros(rows(indin),1);
  
  mul=1;
  for ii=1:columns(indin);
    iout=iout+mul*indin(:,ii);
    mul=mul*sz(ii);
  endfor
  iout=iout+1;
endfunction

  
  
  

Attachment: tmpcummax.m
Description: Binary data

Attachment: natrixindexdecomposemy.m
Description: Binary data

Attachment: natrixindexcomposemy.m
Description: Binary data

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Octave-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to