The 2022 MATLAB Mini-Hack is currently underway where MATLAB users submit 280 characters of code in order to produce a cool image. It's similar to last year's competition but this year you can also use functions from the Image processing toolbox and from File Exchange if you want to. An entry from Tim Davis, Professor of Computer Science at A&M University and author of SuiteSparse among many other things, caught the eye of a few of us earlier today. Here's the code
[u, v] = meshgrid (linspace (0, 2*pi, 512));
z = v/(4*pi) - s.*sin(u);
A great result from just a few lines of code.
t = .1 - s.*(1+cospi(u)) ;
sinpi and cospi
The functions sinpi and cospi were released in MATLAB R2018b and, as well as helping us reduce character count in this code (which, although fun, isn't really all that useful!), they compute sin(x*pi) and cos(y*pi) without explicitly computing x*pi. Ultimately this is more accurate than doing the same thing with the traditional cos and sin functions because the floating point value of pi is an approximation of π. An obvious demonstration of this is
Bobby removed the need for meshgrid by using implicit expansion, released in R2016b, and discussed in detail by Steve Eddins here and here.
Although not used in this code, implicit expansion can also remove the need for functions like ndgrid, repmat and bsxfun in many cases. For example, here's an example of how to get the same results as ndgrid using implict expansion
[x y] = ndgrid(1:3, 4:6);
The Bobby Cheng Challenge
Bobby made the assertion that many entries in the MATLAB Mini Hack could be simplified using the functions shown in this blog post. He challenged me to find some. Will you help me? Find an entry where the functionality mentioned in this blog post would reduce character count, create a remix and post the link to it as a comment to this blog post.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.