From help-request at octave dot org Wed Feb 9 12:30:20 2005 Subject: Re: Access the neighbors of an element From: Mike Miller To: Joerg Sommer cc: Help-Octave List Date: Wed, 9 Feb 2005 12:28:27 -0600 (CST) This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---559023410-342241519-1107973707=:29035 Content-Type: TEXT/PLAIN; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE I'm not clear on what you're doing, but does this help? For matrix X: [n,m] =3D 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 =3D [-n, n, -1, 1] + pos(t); > if any(neighbor(4) =3D=3D down) > neighbor(4) =3D []; > end > if any(neighbor(3) =3D=3D up) > neighbor(3) =3D []; > end > if neighbor(2) > n*n > neighbor(2) =3D []; > end > if neighbor(1) < 1 > neighbor(1) =3D []; > end > Y_energ =3D X_energ + 2*(length(neighbor) -... > =09 =09 =09 2*sum( X(pos(t)) ~=3D X(neighbor) )); > > But this is slower than > Y_energ =3D 2* (n*(n-1)... > - sum(sum( Y(1:n-1, :) =3D=3D Y(2:n, :) ))... > - sum(sum( Y(:, 1:n-1) =3D=3D Y(:, 2:n) )) ); > > with n=3D20. 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=F6rg. > > -- > Es ist au=DFerdem ein weit verbreiteter Irrtum das USENET "helfen" soll. > Tats=E4chlich wurde USENET nachweislich zur pers=F6nlichen Belustigung > seiner Erfinder geschaffen. > J=F6rg 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 > ------------------------------------------------------------- > ---559023410-342241519-1107973707=:29035-- ------------------------------------------------------------- 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 -------------------------------------------------------------