Sunday, July 12, 2009

Fun with Matlab, why

Lately I've been running a few Matlab programs that take about 5 minutes to finish. Naturally I continue to use my computer - but then I don't know when its finished. To solve this problem I add a bit of code to the end of all my programs which will play a sound - like the beep of a microwave, or the ding of a toaster to let me know that my numbers are fully crunched.

If you would also like to have this for your own programs just slip the following into the end.

%% To play a sound
T=419*2; T2=419*4; st=[1:.3:T]; t2=[1:.3:T2];
S=(sin(t*pi/T).^.2+sin(t*pi/T).^2).*sin((t+sin(t/2)*.2).^1.0)/2;
S2=(sin(t2*pi/T2).^.4+sin(t2*pi/T2).^4).*sin((t2+sin(t2/2)*.2)*1.5)/2;
sound([S S],8192); sound(S2,8192);

Another great thing to do in Matlab: type "why" into the command line (without quotes) and see what it answers. Just the word 'why'. Now I would love to show you an example (they are quite funny) but the computer I'm using doesn't have Matalb. The person who posts a comment with the longest answer that matlab gives to that question wins.

19 comments:

  1. You can also play an arbitrary .wav file using some of Matlab's built-in functions designed for that purpose. eg:

    [y,Fs] = wavread(wavFilename);
    playblocking(audioplayer(y, Fs));

    ReplyDelete
  2. A tall and good and not excessively rich and bald and very smart and good tall and tall and terrified and rich and not very terrified and smart and tall and young hamster insisted on it.

    ReplyDelete
  3. No wait, I just got a better one.

    Some very rich and tall and smart and rich and terrified and rich and very young and good and rich and very tall and tall and smart and terrified and not excessively bald and rich and smart and not very young and bald and tall and smart and terrified and rich and tall and young and bald and good and terrified system manager knew it was a good idea.

    This one isn't long, but it's random:

    In the beginning, God created the heavens and the earth...

    ReplyDelete
  4. Be sure to enter: edit why
    to see the guts of the thing.

    ReplyDelete
  5. If you type why(248418) you get (MATLAB Version 7.0.1.15):
    Some not excessively good and rich and rich and good and very good and rich and tall and not excessively tall and rich and good and smart and not excessively good and terrified and terrified and tall and not excessively good and not excessively young and young and terrified and smart and bald and not excessively good and good and smart and not excessively tall and bald and rich and bald and tall and rich and good and young and terrified and very young and terrified and good and not very bald and good and tall and tall and very bald and not excessively rich and bald and bald and very tall and very rich and good and very tall and good and terrified and tall and very tall and good and young and very bald and rich and good and bald and not excessively rich and bald and not very rich and tall and very smart and tall and terrified and not excessively young and not excessively tall and very bald and rich and young and tall and not very terrified and smart and rich and young and terrified and terrified and rich and smart and tall and terrified and young and rich and tall and terrified and smart engineer knew it was a good idea.

    ReplyDelete
  6. just a guess but do you think its possible matlab was ghost programmed and its the creators way of saying screw you or is it some sort of code to be decrypted who knows

    ReplyDelete
  7. Alan,
    You can actually see the source code for the 'why' function. I think it's kind of just a silly thing they included. If you type 'edit why' at the Matlab prompt you can see the code.

    ReplyDelete
  8. 挺有意思

    ReplyDelete
  9. According to Google Translate [link] the anonymous comment means "Quite interesting." Thanks!

    ReplyDelete
  10. I found this one pretty quickly

    length(why(1769578)) = 1325

    The bald and good and bald and good and young and not excessively good and tall and not excessively young and good and rich and very rich and young and good and tall and rich and terrified and smart and rich and good and not very tall and smart and good and bald and good and terrified and not excessively rich and good and smart and bald and bald and good and not excessively tall and smart and not very young and very bald and tall and bald and terrified and smart and good and young and very young and smart and very smart and young and terrified and tall and not very terrified and young and good and not very smart and bald and bald and terrified and very terrified and not very tall and not excessively smart and rich and smart and not very terrified and rich and tall and young and rich and young and terrified and young and smart and very young and not excessively bald and terrified and very young and not excessively young and good and tall and good and not very bald and young and bald and good and very young and very good not excessively bald smart and very bald and young and rich and bald and good and not very bald very good and tall mathematician obeyed the young and rich and tall and good and young and terrified and bald and good and rich and terrified and very rich and bald and tall and tall programmer.

    then i googled and found that i had been outdone by marky at http://www.mathworks.com/matlabcentral/newsreader/view_thread/164966

    he gives,

    length(why(3282186255)) = 1782

    Some rich and very smart and young and bald and very young and smart and bald and terrified and rich and terrified and very bald and smart and not very smart and terrified and terrified and tall and smart and not very bald and bald and tall and smart and good and smart and good and rich and smart and young and very tall and young and not excessively smart and terrified and terrified and not very young and bald and smart and bald and very rich and terrified and young and young and terrified and rich and terrified and good and very tall and rich and not excessively young and terrified and young and not very tall and tall and bald and good and tall and smart and young and good and rich and terrified and good and bald and terrified and very tall and tall and good and terrified and not excessively tall and not very bald and young and young and not very good and smart and good and young and good and good and bald and not excessively young and smart and terrified and young and young and young and young and bald and young and good and young and terrified and tall and rich and young and rich and very terrified and terrified and good and very terrified and young and bald and not very good and smart and good and very good and young and young and terrified and young and young and young and terrified and rich and bald and young and good and bald and rich and good and not excessively good and not very rich and young and very bald and young and terrified and terrified and good and rich and young and good and smart and not very tall and good and terrified and very good and very rich and good and tall and good and rich and rich and smart and tall and good and bald and good and rich engineer obeyed some smart and rich very good and not excessively good and bald hamster.

    Ig Nobel comittee -- i hope you're getting this.

    ReplyDelete
  11. It's an Easter egg; MATLAB is full of them.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. I'm too lazy to actually compete in your mini-contest, so I'll get a computer to do it for me. Here ya go, fella.


    longest = 0;
    for ii = 1:2^2^2^2^2^2^2^2^2^2
    poop = strcat('why(',num2str(ii),')');
    megatron = evalc(poop);
    megatron_length = length(megatron);
    if megatron_length>longest
    longest = megatron_length;
    master_and_commander = megatron;
    disp(master_and_commander)
    end
    end

    ReplyDelete
  14. I think I win with this one:

    for i=1:1000
    why
    end

    change the 1000 to any length.

    ReplyDelete
  15. Hey Giuseppe, its looks to me like Amir has you beat fair and square! lol

    ReplyDelete
  16. But at least Giuseppe's solution can be computed... There's gotta be a way to parse the code to figure out what the longest is.

    ReplyDelete
  17. Because the code for why (Matlab 2010b) generates an LALR grammar, there is a theoretical possibility of an infinitely sized output. However, your computer will eventually run out of memory, and it's not guaranteed that a random number exists in a however-many-bit space that would even generate that large of a result. So, you'd need to run code similar to what Amir earlier suggested over all 2^whatever possibilities to determine the actual maximal result.

    ReplyDelete
  18. I think the best answer is not the longest one. The answer I like most is "why not ?" :)

    ReplyDelete
  19. try this:
    load handel
    sound(y, Fs)

    ReplyDelete