views:

62

answers:

3

From the exercises in a book I am using to learn MATLAB:

Given x = [3 15 9 12 -1 0 -12 9 6 1], provide the command(s) that will

  • A) set the values of x that are positive to zero

  • B) set values that are multiples of 3 to 3 (rem will help here)

  • C) multiply the values of x that are even by 5

  • D) extract the values of x that are greater than 10 into a vector called y

  • E) set the values in x that are less than the mean to zero

  • F) set the values in x that are above the mean to their difference from the mean

A: 

Question a) will teach you the following elements:

  • find a function that returns indexes given a condition, in your case x>0
  • use indexing in order to set selected values in x to 0

to be continued ...

zellus
A: 
x = [3 15 9 12 -1 0 -12 9 6 1]

vi = (x < 0) % statement that returns a boolean, gives a vector like
             % [0 0 0 0 1 0 1 0 0 0]

x(vi) = -x(vi) % does the operation (negating in this case) on the relevant
               % values of x (those with a 1 from above)

Without actually doing your homework, they all follow the above pattern.

MatlabDoug
A: 

I agree with the comments to your question, that is not necessarily the right way to go if you really want to learn something.

As to answer your question, MATLAB has a fantastic function browser I strongly suggest you take a look at it. With well chosen keywords you can go a long way. :)

posdef

related questions