From help-request at octave dot org Wed Feb 9 12:05:09 2005 Subject: Access the neighbors of an element From: Joerg Sommer To: help at octave dot org Date: Wed, 9 Feb 2005 16:29:18 +0000 (UTC) Hi, I want to compare the neighbors of an element to the element, but this is harder than I thought. My idea: neighbor = [-n, n, -1, 1] + pos(t); if any(neighbor(4) == down) neighbor(4) = []; end if any(neighbor(3) == up) neighbor(3) = []; end if neighbor(2) > n*n neighbor(2) = []; end if neighbor(1) < 1 neighbor(1) = []; end Y_energ = X_energ + 2*(length(neighbor) -... 2*sum( X(pos(t)) ~= X(neighbor) )); But this is slower than Y_energ = 2* (n*(n-1)... - sum(sum( Y(1:n-1, :) == Y(2:n, :) ))... - sum(sum( Y(:, 1:n-1) == Y(:, 2:n) )) ); with n=20. In one sentence: it is faster to compare and count twice nearly 800 elements, than find and access the four neighbors of an element. That's lunatic. How can I make it better? Jörg. -- Es ist außerdem ein weit verbreiteter Irrtum das USENET "helfen" soll. Tatsächlich wurde USENET nachweislich zur persönlichen Belustigung seiner Erfinder geschaffen. Jörg Klemenz , ------------------------------------------------------------- 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 -------------------------------------------------------------