Blog IndexPosts by TagHome

Matlab Tinycode

Posted <2015-09-19 Sat 09:35> by Aaron S. Jackson.

Back in second year of my undergraduate degree, we were asked in our MATLAB algorithms design class to write the smallest piece of code to produce the following:

Result of Dice(5)

Result of Dice(5)

After several attempts my final solution was to encode the face patterns in integers of a matrix. The command spy will plot a logical matrix. This is what I came up with.

function Dice(k)
    hold
    rectangle('Po', [0 0 4 4], 'F', 'k', 'Cu', .2)
    spy(bitget([56 0 62
                32 21 32
                62 0 56], k), 'w', 99)
    axis off

In this "competition", only non-whitespace characters were counted. There were a few things like the function definition, hol and axis which were compulsory. I didn't beat the lecturer's solution but I did come up with a solution which I was very happy with.

Last night I was kind of bored and wanted something to do. I decided I'd try and make a random Voronoi diagram which fits in a Tweet.

Output of Voronoi

Output of Voronoi

I'm not quite as happy with this code compared to my Dice code, but I am still going to share it :)

v=rand(15,3)
[x y]=meshgrid(0:5e-3:1)
hold
scatter(x(:),y(:),2,v(dsearchn(v(:,1:2),[x(:) y(:)]),:))
plot(v(:,1),v(:,2),'k.')

Pretty short! And the output is pretty!

Tags: matlab hacks

Blog IndexPosts by TagHome

Copyright 2007-2017 Aaron S. Jackson (modified: Sun 23 Jul 14:30:51 BST 2017)