Octave Bits

Vectorized Popcount - Sun May 21 22:16:15 CEST 2017


function out = popcountvec(arr)
  if(arr>0)
    out=sum(bitget(arr,1:(log2(arr)+1)));
  else
    out=0;
  endif
endfunction

Sierpinski Triangle - Wed May 17 19:56:01 CEST 2017

Numberphile was running an interesting video in April (see below). They describe an algorithm in which you pick three points that form a triangle, and a fourth point which is your current position. Now, from the current position determine one of the threee edges of the triangle by a random method and walk half way there. Make a dot where you are. Then again determine a random egde and walk half way there, make a dot, and so on.

The output of this algorithm is absolutely astounding - you get a Sierpinski triangle. I wouldn’t have guessed that. Here is some Octave code that does exactly that:


%define some triangle
p=zeros(2,3)
p(1,1)=0
p(1,2)=1
p(1,3)=0.5
p(2,3)=1
%define a starting position
t=[0.5;0.25];
% ...aaaand go:
for n=1:100000;
   t=(t+p(:,randi(3)))/2.0;
   g(:,n)=t;
end;
scatter(g(1,:),g(2,:),1)

And you will get this nice Sierpinski triangle! Astounding!

Image