From bug-octave-request at bevo dot che dot wisc dot edu Tue Jan 6 12:20:55 2004 Subject: Re: Missing ND array functionality From: "John W. Eaton" To: Quentin Spencer Cc: bug-octave at bevo dot che dot wisc dot edu Date: Tue, 6 Jan 2004 12:20:41 -0600 On 21-Dec-2003, Quentin Spencer wrote: | John W. Eaton wrote: | | >On 20-Dec-2003, Quentin Spencer wrote: | > | >| If you have a 3 dimensional array of size (x,y,z), there is not | >| currently a function to "rotate" the dimensions to convert the array to | >| an array of size (y,z,x) (such a conversion in two dimensions would be | >| the same as a transpose). Matlab provides the function "shiftdim" to | >| accomplish this. I think this may be possible using reshape as well. | > | >We have permute and ipermute. Can shiftdim be based on that? | > | > | > | Thanks for pointing those out. Yes, shiftdim can be based partially on | that. For positive arguments, shiftdim would look like this: | | function out = shiftdim(in,N) | | ndims = length(size(in)); | out = permute(in,[N:ndims,1:N-1]); I think that should be out = permute (in, [N+1:ndims, 1:N]); otherwise if N is 1, it seems there won't be any shift. | However, the Matlab function shiftdim also takes a negative argument, | and adds singleton dimensions (sort of the opposite of the "squeeze" | function). For example if A is a 1xN vector, shiftdim(A,-1) is a 1x1xN | array. I think that part could be handled using reshape, but it's not | obvious to me exactly how. How about something like function out = shiftdim (in, N) if (N < 0) orig_dims = size (in); singleton_dims = ones (1, -N); out = reshape (in, [singleton_dims, orig_dims]); elseif (N > 0) ndims = length (size (in)); out = permute (in, [N+1:ndims, 1:N]); else out = in; endif endfunction Can you (or someone) write a complete implementation of this with some error checking and a doc string? You'll also need to handle the case of [out, nls] = shiftdim (in) which removes the leading singleton dimensions and returns the number of leading singleton dimensions in nls. I think you can also use reshape for doing that. Thanks, jwe ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------