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)
    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 :)

[x y]=meshgrid(0:5e-3:1)
scatter(x(:),y(:),2,v(dsearchn(v(:,1:2),[x(:) y(:)]),:))

Pretty short! And the output is pretty!

Related posts:

Tags: matlab hacks

Blog IndexPosts by TagHome

You can verify this page with the following command:
gpg --verify <(curl -s <(curl -s

Copyright 2007-2018 Aaron S. Jackson (compiled: Sun 3 Jun 13:25:02 BST 2018)