help-octave
[Top][All Lists]
Advanced

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

Re: elementwise boolean operations


From: Przemek Klosowski
Subject: Re: elementwise boolean operations
Date: Thu, 20 May 2010 18:43:28 -0400
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100330 Fedora/3.0.4-1.fc12 Lightning/1.0b1 Thunderbird/3.0.4

On 05/20/2010 03:51 PM, Judd Storrs wrote:
function m = graycode(n)
   m = false(2^n,n) ;
   m(2,n) = true ;
   for i=1:n-1
     j = n-i ;
     k = 2^i+1:2^(i+1) ;
     m(k,j) = true ;
     m(k,j+1:end) = flipdim(m(1:2^i,j+1:end),1) ;
   endfor
endfunction

Both Gray and straight binary can be seen as recursively
defined: the top left corner is recursively calculated,
and flipped (for Gray) or copied straight into bottom left
corner, and the leftmost column is filled with 00000...11111

function m=graycode(n)
 if (n==1)  m=[0;1];  return; end
 s=2^(n-1);
 m=false(2*s,n);
 m(1:s,2:end)=graycode(n-1);
 m(s+1:end,2:end)=m(s:-1:1,2:end);
 m(s+1:end,1)=true;
end

function m=binarycode(n)
 if (n==1)  m=[0;1];  return; end
 s=2^(n-1);
 m=false(2*s,n);
 m(1:s,2:end)=binarycode(n-1);
 m(s+1:end,2:end)=m(1:s,2:end);
 m(s+1:end,1)=true;
end

They are competitive with the non-recursive version for time.

Improvement suggestions welcome, e.g. I wonder if there is a nicer way of flipping the rows than

m(s+1:end,2:end)=m(s:-1:1,2:end);


reply via email to

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