From help-request at octave dot org Fri Feb 11 04:31:05 2005 Subject: Re: Access the neighbors of an element From: =?iso-8859-1?Q?J=F6rg?= Sommer To: Przemek Klosowski Cc: help at octave dot com Date: Thu, 10 Feb 2005 14:24:26 +0100 Przemek Klosowski schrieb am Wed 09. Feb, 15:57 (-0500) : > I can't figure out the intent of your code (I don't know what pos(t) is, > and what's down/up and n). On the general description, I am guessing that > you are simulating something like a spin system on a rectangular lattice, Yes, that's right. I have a n*n matrix with -1 and 1 and simulate their behaviour -- I know it as the ising modell. I energie of the matrix is the number of unequal neighbors minus the number of equal neighbor, where a neighbor is the right M(i,j+1) or the lower M(i+1,j) element in the matrix. For the whole matrix I can determine the energie with: X_energ = 2* (n*(n-1)... - sum(sum( X(1:n-1, :) == X(2:n, :) ))... - sum(sum( X(:, 1:n-1) == X(:, 2:n) )) ); Over the time, the matrix changes only at one position. By hand it is much easier to calculate the passage of the energie of one matrix to the other, than calculate the whole energie again. The transit is in O(1), the energie is in O(n^2). My idea was to do the same in octave, but it doesn't work. It's faster to compare 800 elements than find and access 4 elements. > and that neighbors of X(m,n) refer to X(m-1,n), X(m+1,n), X(m,n-1) and X(m,n+1). Yes. > Maybe you want a more arbitrary neighbor configuration... > > If that's your case, then you can do it the way I said, using terms of the form > (X(m,n) == X(m-1,n)). But n and m are chosen randomly and I don't know if m+1, m-1, n-1, n+1 exist. My current way is: % upper and lower bounds of the matrix up = 0:n:n*n; down = 1 + n + up; % calculate all possible neighbors 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 % calculate the energie the "simple" way Y_energ = X_energ + 2*(length(neighbor) - 2*sum( X(pos(t)) ~= X(neighbor) )); note: I found out generating n random number at one time is _much_ faster then generating one random number n times. Because this I generate all my positions before entering the loop. > In general, the linear (or linearized) hamiltonian usually looks like > a 2D convolution: E = sum_i=allNeighbors (a*Si*S0) > > [...] = conv2 (...) What's that? octave:33> conv2 error: `conv2' undefined near line 33 column 1 -- Viele Leute glauben, daß sie denken, wenn sie lediglich ihre Vorurteile neu ordnen. ------------------------------------------------------------- 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 -------------------------------------------------------------