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 |