Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: multi-dimensional indexing Date: Tue, 7 Feb 2012 17:36:11 +0000 (UTC) Organization: Xoran Technologies Lines: 11 Message-ID: <jgrneb$358$1@newscl01ah.mathworks.com> References: <jgh0rp$po9$1@newscl01ah.mathworks.com> <jgm2i6$ej2$1@newscl01ah.mathworks.com> <jgr0j2$f8o$1@newscl01ah.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-06-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1328636171 3240 172.30.248.38 (7 Feb 2012 17:36:11 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Tue, 7 Feb 2012 17:36:11 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1440443 Xref: news.mathworks.com comp.soft-sys.matlab:757029 "Roel H" wrote in message <jgr0j2$f8o$1@newscl01ah.mathworks.com>... > > I'm working with a multidimensional grid, in which I traverse through cells. To keep track of the current cell I use the subscripts in each dimension. After the computations in the current cell, the algorithm needs to advance to neighboring cells. With subscripts, this is pretty straightforward, just do+/- 1 in the wanted dimension. In linear indices it is more complicated to get the index of a neighboring cell (which probably would involve transforming back and forth between subscripts and linear indices). > Also the subscript code is more readable than linear indexing code. ================ I can agree with the concerns about code readability. However, if you start with subscripts, the need to transform to linear indices will be inevitable, especially if you are looking for a dimension-independent indexing scheme. If speed is an issue for you, (and it sounds like you might be dealing with an already painfully slow for-loop through the grid elements) you ought to seriously consider reframing things in linear indexing terms. It isn't that hard to do jump between grid neighbors using linear indexing. In an MxN grid, you move up/down by adding +/-1 and you move right/left by adding +/-N The desired increments are easy enough to pre-compute.