help-octave
[Top][All Lists]
Advanced

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

Re: Having trouble with global array variables


From: Ben Abbott
Subject: Re: Having trouble with global array variables
Date: Sun, 06 Jul 2008 20:01:55 -0400


On Jul 6, 2008, at 7:43 PM, Olumide wrote:

Hello -

The following script is supposed to use the user-defined cov() function
to initialize a matrix which is later solved. For some reason, the
global variable position is either not visible or disappears this
function. What am I doing wrong?

Thanks,

- Olumide

############################### SCRIPT ###############################

debug_on_warning(1);
debug_on_error(1);

clear height;
clear sigma;
clear position;
clear B;

global position;

position = [ 0,0,0; 256,0,0; 0,256,0; 256,256,0; 128,128,5 ];
#position = [ 0,0,0; 0,128,0; 256,128,0; 256,0,0; 0,256,0; 256,256,0;
128,128,5 ];
zeroMatrix = [ 0,0,0; 0,0,0; 0,0,0 ];
zeroVec = [ 0; 0; 0];
dim = size(position)(1);

################### Subroutines ###################

function dist = r( i , x , y )
  global position;
  dist = sqrt( ( x - position(i,1) )^2 + ( y - position(i,2) )^2 );
endfunction


function c = covariance( i, x, y )
  rad = r( i, x, y );
  c = rad^2 * log10(rad);
endfunction


function c = cov( i, j )

global position;

  if(i == j)
    c = 0;
  else
    rad = r( j, position(i,1), position(i,2) );
    c = rad^2 * log10(rad);
  endif
endfunction

################### Construct & solve Matrix ###################

for i = 1:dim
  B(i, 1) = 1;
  B(i, 2) = position(i,1);
  B(i, 3) = position(i,2);

  height(i,1) = position(i,3);

  for j = 1:i
    sigma(i,j) = sigma(j,i) = cov(i,j);
  endfor
endfor

M = [[sigma, B];[B',zeroMatrix]];
I = [height;zeroVec];

O = M\I;

The line "global position" should occur in the original script as well as in each function that uses it.

To be safe, be sure to insert the line "global position" ahead of any line that uses the variable "position".

I haven't tried to run your script, but I did insert the lines where they belong ... hopefully, I got it all right.

Ben



reply via email to

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