EGR 1302
RA – Ch 6 (5
th
Ed.)
Chapter 6 Reading Assignment – Part 1
Instructions
Read Chapter 6 of MATLAB – An Introduction with Applications
by Gilat, and find the answers to
the following questions. Write down the answers to each question on this handout.
Section 6.1
1.
What is the difference between a relational operator and a logical operator?
2.
What is the difference between “=” and “==”?
3.
What is the result when two arrays are compared using a relational operator?
4.
Review the first gray-highlighted section of example code. Why is the first value of the “d” vector zero?
5.
Notice: The book has an incorrect statement in the first bullet after the gray-highlighted section containing variables “r” through “w”. Numerical arrays CAN be used to address other arrays, but they behave differently than a logical (or Boolean) array. Example:
>> x = [5 12 -4 6]; y = [1 12 -4 6];
>> z = (x == y)
z =
1×4 logical array
0 1 1 1
>> x(z) ans =
12 -4 6
>> q = [1 4];
>> x(q) ans =
5 6
1
z is a logical or “Boolean” array of 0’s and 1’s. The 1’s appear where the comparison “x==y” is true.
Using z to index (or address) the vector x, the result is the values of x where z is 1. In this case, the last three.
q is a numerical array. Using q to index the vector x, the result is the values of x at the locations specified by
the numbers of q. In this case, locations 1 and 4.