Accessing Arbitrary Dimensions

Bill Denney wsloand at gmail.com
Sun Feb 24 21:09:34 CST 2008


Hi,

I'm trying to run code where I don't know the dimension that I'm wanting 
to use a priori.  In other words, I want to take as an input argument 
the dimension that I want to work on with a function that works 
similarly to max (hhigh.m, attached).

A similar example to what I want to do is:

function y = test(x, dim)
  n = 5;
  sz = size(x);
  for i = 1:sz(dim)
    ## the next line should be corrected to
    points = x(:,:,:,max(1,i-n+1),:,:)
    y = max(x(points)
  endfor

I had the idea to use sub2ind to compute all the points that I need to 
use, but I was wondering if there was a simpler (and possibly faster) 
way than something like this in the loop:

sz = size (x);
idx = max(1, i-n+1)
sz_tmp = mat2cell(ones (length (idx),1)*sz, 1, length (sz));
sz_nodim = sz;
sz_nodim(dim) = [];
points = zeros (length (idx), prod (sz_nodim));
for j = 1:length(idx)
  sz_tmp2 = sz_tmp;
  sz_tmp2{dim} = idx;
  points(j,:) = sub2ind(sz, sz_tmp2{:});
endfor

All of the above was typed directly into this e-mail, and I'm pretty 
sure that it doesn't do quite what I'm wanting, but hopefully it gets 
the idea across.

Thanks,

Bill
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: hhigh.m
Url: https://www.cae.wisc.edu/pipermail/help-octave/attachments/20080224/0a2c19e9/attachment-0001.ksh 


More information about the Help-octave mailing list