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)

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

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!

Wanting to leave a comment?

Comments and feedback are welcome by email (aaron@nospam-aaronsplace.co.uk).

Tags: matlab hacks

Blog IndexPosts by TagHome

Copyright 2007-2024 Aaron S. Jackson (compiled: Mon 30 Sep 12:34:19 BST 2024)