Chapter 2 Data Handling MATLAB stores numbers,strings,and logical values into variables.Variables can be either simple,i.e.,referring to one data type only,or complex,i.e.,referring to different data types at the same time.Furthermore,variables can be imported, exported,and manipulated at will. Types of Variables (Logical Values,Strings,NaN, Structures.Cells) In the previous chapter we saw how to store and operate with numbers;however sometimes we need to deal with other of data vith these types s as well. Logical Variables Logical data(or simply logical)represent the logical TRUE state and the logical FALSE state.Logical variablesare the variables in which logical ataare stored. Logical variables can assume only two states: represented by True,always represented by a nonzero object.Usually.the digit 1 is used for TRUE.However,MATLAB treats any nonzero value as TRUE. There are two ways to create a logical variable.The first is to explicitly declare it using the logical(x)function;this function converts the elements of the array X into logical data types. ringer Science+Business Media.LLC2012
M. Borgo et al., MATLAB for Psychologists, 25 DOI 10.1007/978-1-4614-2197-9_2, © Springer Science+Business Media, LLC 2012 MATLAB stores numbers, strings, and logical values into variables. Variables can be either simple, i.e., referring to one data type only, or complex, i.e., referring to different data types at the same time. Furthermore, variables can be imported, exported, and manipulated at will. Types of Variables (Logical Values, Strings, NaN, Structures, Cells) In the previous chapter we saw how to store and operate with numbers; however, sometimes we need to deal with other types of data such as strings, logical values, and so on. MATLAB can be used to deal with these types of variables as well. Logical Variables Logical data (or simply logical ) represent the logical TRUE state and the logical FALSE state. Logical variables are the variables in which logical data are stored. Logical variables can assume only two states: • False, always represented by 0; • True, always represented by a nonzero object. Usually, the digit 1 is used for TRUE. However, MATLAB treats any nonzero value as TRUE. There are two ways to create a logical variable. The fi rst is to explicitly declare it using the logical(X) function; this function converts the elements of the array X into logical data types. Chapter 2 Data Handling
26 2 Data Handling eLogic Var·1og1ca2a >×■[0,1,2,31: >x_becomelogic logical(x) x_becomelogic 0 7 1 1 >whos Name size Bytes class Attributes Test_Logic_Var 1×1 ans 1x4 1x4 double x_becomelogic 1×4 logical By typing whos at that MATLAB prompt,you can see that Test Logic_var is a 1x I matrix(a scalar value)of logical values.Vector x contains numbers different from 0 and 1.When the logical function is applied to the vector x,the nonzero values are marked as 1,that is.TRUE values. We can also create logical variables indirectly,through logical operations,such as the result ofa mpar son between two nu Th s retum logical >5>3 ans 1 >10>45 ans= 0 Since5 is indeed gre son is true:ho wever relational operator,retuming logical data types as a result. When a vector or a matrix is involved in a logical or relational expression,the comparison is carried out element by element.Therefore,if we are checking whether the content of an array is greater than 5,the comparison is made for each element of the array,and the resulting logical array has the same length as that of the array we are checking.The following example shows how this works: a=[1,3,5,9,111 >b=[3,4,5,8,10]: >>c=a>3: >>D=a>b: >whos a b c d Ccommand examples are intended followed by the <ENTER>key.i.e. >clear all means:Type lea all after the >>prompt,and press the Enter key
26 2 Data Handling >> clear all;1 >> Test_Logic_Var = logical(1); >> x = [0,1,2,3]; >> x_becomelogic = logical(x) x_becomelogic = 0 1 1 1 >> whos Name Size Bytes Class Attributes Test_Logic_Var 1x1 1 logical ans 1x4 4 logical x 1x4 32 double x_becomelogic 1x4 4 logical By typing whos at that MATLAB prompt, you can see that Test_Logic_Var is a 1 × 1 matrix (a scalar value) of logical values. Vector x contains numbers different from 0 and 1. When the logical function is applied to the vector x , the nonzero values are marked as 1, that is, TRUE values. We can also create logical variables indirectly, through logical operations, such as the result of a comparison between two numbers. These operations return logical values. For example, type the following statement at the MATLAB prompt: >> 5 > 3 ans = 1 >> 10 > 45 ans = 0 Since 5 is indeed greater than 3, the result of the comparison is true; however, 10 is not greater than 45, and hence the comparison is false. The operator > is a relational operator, returning logical data types as a result. When a vector or a matrix is involved in a logical or relational expression, the comparison is carried out element by element . Therefore, if we are checking whether the content of an array is greater than 5, the comparison is made for each element of the array, and the resulting logical array has the same length as that of the array we are checking. The following example shows how this works: >> clear all; >> a = [1,3,5,9,11]; >> b = [3,4,5,8,10]; >> c = a > 3; >> D = a > b; >> whos a b c d 1 Ccommand examples are intended followed by the <ENTER> key, i.e.: >> clear all means: Type clear all after the >> prompt, and press the Enter key
Types of Variables(Logical Values,Strings.NaN.Structures.Cells) 27 Name Size Bytes Class Attributes a 1×5 40 double 6 1×5 40 double 1×5 5 logical 1×5 5 logical Using the command whos you can see that the vectors c and d are logical vectors They are not considered d by vectors but as vectors of logical table lists the by MATLAB MATLAB Examnle Meaning Greater than >c.a>3 show than 3 s which values of a are greater 00111 d sh er tha >d a b e 00011 e a >3 which values of a are greater than or equal to 。 or eaual to 3 01111 f=a>=b of b 00111 Less than >g=b<3 g shows which values of b are less than 3. g 00000 shows which h =a<b h 11000 Less than or >>1=b<=3 i shows which values of a are less than or equal to 1= equal to 3 10000 i shows which values of a are less than o equal to the values in the same position 11100 Equal to >>k=a==9 k shows which values of a are equal to 9 I shows which values of a 00010 >>1=a==} ator ==is differen 1= 00100 when you use it:a=b is different from a =b Not equal to >>k=a-=9 k shows which values of a are equal to 9 11101 aa8o >1=a= 1■ 11011
Types of Variables (Logical Values, Strings, NaN, Structures, Cells) 27 Name Size Bytes Class Attributes a 1x5 40 double b 1x5 40 double c 1x5 5 logical d 1x5 5 logical Using the command whos you can see that the vectors c and d are logical vectors. They are not considered by MATLAB as numeric vectors but as vectors of logical values. The following table lists the relational operators used by MATLAB. MATLAB operator Description Example Meaning > Greater than >> c = a > 3 c = 0 0 1 1 1 >> d = a > b d = 0 0 0 1 1 c shows which values of a are greater than 3 d shows which values of a are greater than the values in the same position of b >= Greater than or equal to >> e = a >= 3 e = 0 1 1 1 1 >> f = a >= b f = 0 0 1 1 1 e shows which values of a are greater than or equal to 3 f shows which values of a are greater than or equal to the values in the same position of b < Less than >> g = b < 3 g = 0 0 0 0 0 >> h = a < b h = 1 1 0 0 0 g shows which values of b are less than 3. h shows which values of a are less than the values in the same position of b <= Less than or equal to >> i = b <= 3 i = 1 0 0 0 0 >> j = a <= b j = 1 1 1 0 0 i shows which values of a are less than or equal to 3 j shows which values of a are less than or equal to the values in the same position of b == Equal to >> k = a == 9 k = 0 0 0 1 0 >> l = a == b l = 0 0 1 0 0 k shows which values of a are equal to 9 l shows which values of a are equal to the values in the same position of b Note: the logical operator == is different from the assign operator = ! Pay attention when you use it: a = b is different from a == b ~= Not equal to >> k = a ~= 9 k = 1 1 1 0 1 >> l = a ~= b l = 1 1 0 1 1 k shows which values of a are equal to 9 l shows which values of a are equal to the values in the same position of b
28 2 Data Handling ary data outliers,and you want to have the average calculated without them.In this case,you can use the logical operators to average only the data you want to include and according to a cutoff value of your choice (later in the text you will see more examples). MATLAB uses several logical operators such as &I.and~.The following table shows their use by considering the vectors a,b.c,and d implemented above MATLAB operator Description Truth table Example Meaning Logical AND A B A&B >>m=a s b m contains the 00 0 41111 ment-by-element 010 >n=cs d and b are all different n 100 00011 111 n contains th AND OF logical vectors e and d Logical OR AB AB 0=a1b 000 11111 ectors a and b.The 01 1 p-cld fa and b ar 10 1 00111 111 OR operation of the logical vectore. Logical NOT >q -a q contains the element- 01 8000 ement NO 10 >>r=-c are TRUE values.The result is a vector of Is r contains the element by-element NOT logica on of the logica In many cases we need to perform multiple comparisons at once.This is,of course,possible in MATLAB,but we need to follow the MATLAB rules if we do not want to get the show this lt's test whether a variable falls within the range fromto2 We might be tempted to prompt:0<<2.However
28 2 Data Handling Logical operators are useful in different occasions, such as in preliminary data analysis. Let’s say you need to calculate the average of a data set that includes some outliers, and you want to have the average calculated without them. In this case, you can use the logical operators to average only the data you want to include and according to a cutoff value of your choice (later in the text you will see more examples). MATLAB uses several logical operators such as &, |, and ~. The following table shows their use by considering the vectors a , b , c, and d implemented above. MATLAB operator Description Truth table Example Meaning & Logical AND >> m = a & b m = 1 1 1 1 1 >> n= c & d n = 0 0 0 1 1 m contains the element-by-element AND of the vectors a and b . The values of a and b are all different from zero, that is, they are TRUE values. The result is a vector of 1s n contains the elementby-element AND of the logical vectors c and d | Logical OR >> o = a | b o = 1 1 1 1 1 >> p = c | d ans = 0 0 1 1 1 o contains the elementby-element OR of vectors a and b . The values of a and b are all different from zero; they are TRUE values p contains the element-by-element OR operation of the logical vector c . ~ Logical NOT >> q = ~a q = 0 0 0 0 0 >> r = ~c r = 1 1 0 0 0 q contains the elementby-element NOT. Values in a are all different from zero; they are TRUE values. The result is a vector of 1s r contains the elementby-element NOT logical operation of the logical vector c In many cases we need to perform multiple comparisons at once. This is, of course, possible in MATLAB, but we need to follow the MATLAB rules if we do not want to get the wrong result. To show this, let’s test whether a variable x falls within the range from 0 to 2. We might be tempted to prompt: 0 < x < 2. However
Types of Variables(Logical Values,Strings.NaN.Structures.Cells) 29 using this syntax leads to an incorrect result.Indeed,if,let's say.x is equal to 3, hence outside our range.MATLAB returns 1.which is TRUE. >■3: >0<×<2 ans 1 Why does MATLAB return an incorrect result?Because MATLAB makes the comparisons in succession.It first compares x with 0,and because 3 greater than 0. the result of the comparison is true,i.e.1.Then MATLAB compares the result,i.e. 1.with 2.Because 1 is less than 2.the result of the operation is true.So we need to use a different syntax to obtain the correct result.Multiple comparisons like the previous one have to be written in the following way: >(0<x&(x<2) ans 0 Let us see how to use logical values to target different positions in a vector.We have already seen in Chap.I that the elements of a vector can be referenced by means of another numeric vector:we can acc e of the vector's ele r vector to int to the 黑女 we want.For e want the hird and fifth po we can use anothe >clear all >a=[3,4,7,9,111: >>b=[3,51: >a(b) 7 11 When programming.we use logical index vectors in several conetLet's say that we h ve a numeric vector and we want to store in a second vector only the val- ues outside the range 6-2.We can do so in this way: >clear all; >a=[1,-2,5,7,3,261: >c=(a>=7)1(a<2) c= 1 0101 >d=a(c) 1 -2726
Types of Variables (Logical Values, Strings, NaN, Structures, Cells) 29 using this syntax leads to an incorrect result. Indeed, if, let’s say, x is equal to 3, hence outside our range, MATLAB returns 1, which is TRUE. >> x = 3; >> 0 < x < 2 ans = 1 Why does MATLAB return an incorrect result? Because MATLAB makes the comparisons in succession. It fi rst compares x with 0, and because 3 greater than 0, the result of the comparison is true, i.e., 1. Then MATLAB compares the result, i.e., 1, with 2. Because 1 is less than 2, the result of the operation is true. So we need to use a different syntax to obtain the correct result. Multiple comparisons like the previous one have to be written in the following way: >> (0 < x) & (x < 2) ans = 0 Let us see how to use logical values to target different positions in a vector. We have already seen in Chap. 1 that the elements of a vector can be referenced by means of another numeric vector; we can access some of the vector’s elements using another vector to point to the positions we want. For example, if we want the elements in the third and fi fth positions of a vector a of size 5, we can use another vector b as an index vector pointing to the positions we need: >> clear all >> a=[3, 4, 7, 9, 11]; >> b=[3,5]; >> a(b) a= 7 11 When programming, we use logical index vectors in several contexts. Let’s say that we have a numeric vector and we want to store in a second vector only the values outside the range 6–2. We can do so in this way: >> clear all; >> a = [1, -2, 5, 7, 3, 26]; >> c = (a>=7) | (a<2) c = 1 1 0 1 0 1 >> d=a(c) d = 1 -2 7 26