5 Using conditional operators
In MATLAB create a new script by clicking on the New Script button found in the upper-left corner of the MATLAB desktop, or by using the keyboard shortcut Ctrl + N. The MATLAB editor should open with a blank script.
Save the script by clicking on the Save button in the editor, or by using the keyboard shortcut Ctrl + S. Save the script using the filename labE.m.
Recall that names in MATLAB are case-sensistive: The script saved as labE.m is different than the script saved as LABE.m.
Answer the following questions in your script.
(a)
Create the following row vector x:
x = [7 9 -8 9 3 -8 -5 1 10 10];
Using a conditional operator, create a row vector a where the elements of a are:
- equal to
1where the corresponding elements inxare greater than5 - equal to
0everywhere else.
Do not manually set the indiviudal elements of a.
(b)
Using a conditional operator, create a row vector b where the elements of b are:
- equal to
1where the corresponding elements inxare equal to10 - equal to
0everywhere else.
Do not manually set the indiviudal elements of b.
(c)
Using a conditional operator, create a row vector c where the elements of c are:
- equal to
1where the corresponding elements inxare not equal to10 - equal to
0everywhere else.
Do not manually set the indiviudal elements of c.
(d)
Using a conditional operator, create a row vector d where the elements of d are:
- equal to
1where the corresponding elements inxare even - equal to
0where the corresponding elements inxare odd
Do not manually set the indiviudal elements of d.
(e)
Create a new vector e that is a copy of x.
Using a conditional operator and logical indexing, set all of the negative elements of e to the value 0.
Do not manually set the indiviudal elements of e.
(f)
Using a conditional operator and the sum function, create a variable f that is equal to the number of elements of x that are less than 2.
Do not manually set the value of f; you must use a a conditional operator and the sum function.
(g)
Look up the help documentation for the MATLAB function find.
Using the find function, create a variable g that is equal to the indices of the elements of x that are less than 2.
Do not manually set the individual elements of g; you must use a a conditional operator and the find function.
(h)
Create a new vector h that is a copy of x.
Using a conditional operator and the find function, set all of the values of h that are greater than or equal to 7 to the value 100.
Do not manually set the indiviudal elements of h.