From help-request at octave dot org Fri Feb 11 19:05:32 2005 Subject: Re: Access the neighbors of an element From: Joerg Sommer To: help at octave dot org Date: Sat, 12 Feb 2005 00:55:26 +0000 (UTC) Francesco Potorti` wrote: >>My idea was to do the same in octave, but it doesn't work. It's faster to >>compare 800 elements than find and access 4 elements. > > In fact, it works, as soon as you remember that O(1) and O(n^2) need at > least a multiplicative constant in order to produce times. If you :) Yes, you are right. But I wonder, that the constant before O(1) is so greate, that it blast O(n^2) (with n=20) away. >>> If that's your case, then you can do it the way I said, using terms of the form >>> (X(m,n) == X(m-1,n)). >> >>But n and m are chosen randomly and I don't know if m+1, m-1, n-1, n+1 >>exist. > > Create a matrix with guard (first and last) columns and rows, which are > not used for computations, but only to be sure that m+1, m-1, n+1, n-1 > all exist. Fille them with values that do not change the results of > your computations. For example, you can use "*" instead of "==" when > computing the energy and set the guard values to 0, so they do not > interfere. Super idea! Really great idea. I'm enthused! I've changed my code this way and it is 33% faster than the old version. But now it is evident, that the code can not become better. Two lines are the end. ;-) neighbor = [-(n+2), n+2, -1, 1] + pos(t); Y_energ = X_energ + 2 * sum( X(pos(t)) .* X(neighbor) ); Is it possible to tell octave it should use integer data types? I don't need floating point operations. Good night, Joerg. -- "UNIX was not designed to stop people from doing stupid things, because that would also stop them from doing clever things." -- Doug Gwyn ------------------------------------------------------------- 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 -------------------------------------------------------------