Potty Page

March 9, 2007

A puzzle for you...

It's dead simple... I think you should all try it even if I lose you a little in the explanation... (or indeed have never programmed before!)

In the lab today (Java) someone asked me about MATLAB... (these are both programming languages). The question was...

I've got a vector full of zeros and ones - I want to make the zeros into ones and the ones into minus ones... how?

So there you are... how would you do it?

In English... he's got a list of numbers like this 0, 1, 0, 0, 1, 1.... and wants to make this into a list that's 1, -1, 1, 1, -1, -1...

Here's an example for you... go through each item in the list and if the number is 0 set it to 1 and if it's a 1 set it to a -1 - this way is kinda pants if you ask me)

As a hint... as MATLAB lets you do stuff to the whole vector at once... For example... say I've got my 0, 1, 0, 0, 1, 1 vector... and I want to add five to all of the numbers I'd do something like y=x+5 - which would give me 5, 6, 5, 5, 6, 6 (with x being the original vector and the result being put in y...

So... how would you do it? (There's probably no wrong answer)

Posted by Ed at March 9, 2007 6:55 PM | Ramble |
Comments and TrackBacks

TrackBack URL for this entry:
http://www.bloged.co.uk/mt/mt-tb.cgi/1641

How about y=-2x+1

That would seem to work.

How did you do it?

Posted by: Tom on March 10, 2007 11:41 AM

Just like you did :-)

Posted by: Ed on March 10, 2007 2:00 PM

Woo, that's the same solution I came up with. Can we have a more difficult one now :)

Posted by: Chris on March 11, 2007 9:28 AM

Well seeing as you asked... here's one I had to come up with in part of my PhD...

I've got a vector full of signed numbers (ie +ve or -ve)...

If it's a -ve number I want there to be a 2 in it's place and if it's +ve I want there to be a 3.

Also simple... because once you've done one, you've done them all!

Posted by: Ed on March 11, 2007 1:43 PM

I also came up with y=-2x+1, and was feeling all smug until I saw that I wasn't alone.

Posted by: Joe on March 11, 2007 8:25 PM
Post a comment