help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Access the neighbors of an element


From: Joerg Sommer
Subject: Access the neighbors of an element
Date: Wed, 9 Feb 2005 16:29:18 +0000 (UTC)
User-agent: slrn/0.9.8.0 (Linux)

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 <address@hidden>, <address@hidden>



-------------------------------------------------------------
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
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]