help-octave
[Top][All Lists]
Advanced

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

Re: Access the neighbors of an element


From: Mike Miller
Subject: Re: Access the neighbors of an element
Date: Wed, 9 Feb 2005 12:28:27 -0600 (CST)

I'm not clear on what you're doing, but does this help?
For matrix X:

[n,m] = size(X)

X(2:n,:) has "up neighbors" X(1:n-1,:)
X(1:n-1,:) has "down neighbors" X(2:n,:)
X(:,2:m) has "left neighbors" X(:,1:m-1) and
X(:,1:m-1) has "right neighbors" X(:,2:m)

Maybe that is irrelevant to your question.

Mike


On Wed, 9 Feb 2005, Joerg Sommer wrote:

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]