[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
-------------------------------------------------------------
- Access the neighbors of an element,
Joerg Sommer <=
- Re: Access the neighbors of an element, Mike Miller, 2005/02/09
- Re: Access the neighbors of an element, Francesco Potorti`, 2005/02/10
- Message not available
- Re: Access the neighbors of an element, Jörg Sommer, 2005/02/10
- Re: Access the neighbors of an element, Francesco Potorti`, 2005/02/11
- Re: Access the neighbors of an element, Joerg Sommer, 2005/02/11
- Re: Access the neighbors of an element, John W. Eaton, 2005/02/11
- Data types (was: Re: Access the neighbors of an element), Joerg Sommer, 2005/02/12
- Data types (was: Re: Access the neighbors of an element), John W. Eaton, 2005/02/12
- Re: Data types (was: Re: Access the neighbors of an element), Paul Kienzle, 2005/02/12
- Re: Data types (was: Re: Access the neighbors of an element), Paul Kienzle, 2005/02/12