Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Matlab Vectors and Their Manipulation, Lab Reports of Computer Science

An introduction to vectors in matlab, explaining their concept, creation methods, and various operations such as scalar arithmetic, logical operations, concatenation, and slicing. It also covers indexing techniques using both predefined and anonymous index vectors.

Typology: Lab Reports

Pre 2010

Uploaded on 08/04/2009

koofers-user-j3m
koofers-user-j3m 🇺🇸

10 documents

1 / 9

Toggle sidebar

Related documents


Partial preview of the text

Download Understanding Matlab Vectors and Their Manipulation and more Lab Reports Computer Science in PDF only on Docsity! Page 1-1 Concepts in Computer Science Implemented in Matlab 1. Vectors 1.1. Concept: Data Collections In Chapter 2, we dealt with the idea of performing mathematical and logical operations on single data items. Here, we consider the concept of grouping data items in general, and then specifically consider one very common way to group data. 1.1.1. Data Abstraction It is frequently convenient to refer to groups of data collectively – “all the temperature readings for May,” or “all the purchases from WalMart.” This allows us not only to move these items around as a group, but also to consider mathematical or logical operations on these groups. For example, we could discuss the average, maximum or minimum temperatures for a month, or that the cost of the WalMart purchases had gone up 3%. 1.1.2. Homogeneous Collection Later, we will encounter collection implementations that allow items in a collection to be of different data types. This first collection type, however, will be constrained to accept only items of the same data type. We refer to collections with this constraint as “homogeneous collections.” 1.2. Matlab Vectors A vector is the simplest means of group a collection of like data items. Initially, we will consider vectors of numbers or Boolean values. Some languages refer to vectors as “linear arrays” or “linear matrices.” As these latter names suggest, a vector is a one- dimensional grouping of data as illustrated in Figure 1. 45 57 66 48 39 71 68. . . . 1 2 3 4 5 n-1 n Value: Index: Figure 1 - a Vector 1.2.1. Definition of Terms We usually refer to the individual items in a vector as its elements. Vector elements have two separate and distinct attributes that make them unique in a specific vector: their numerical value, and their position in that vector. For example, we find the individual number 66 in the 3rd position in the vector in Figure 1. Its value is 66, and its index is 3. There may be other items in the vector with value 66, but no other item will be located in this vector at position 3. Page 1-2 Concepts in Computer Science Implemented in Matlab 1.2.2. Creating a Vector There are two ways to create vectors that are directly analogous to the techniques for creating individual data items: • Creating vectors as a series of constant values or • Producing new vectors by operating on existing vectors. There are three ways to create vectors from constant values: • directly entering the values, e.g. A = [2, 5, 7, 1, 3] (actually, the commas are optional, and frequently omitted), • entering the values as a range of numbers, e.g. B = 1:3:20 where the first number is the starting value, the second number is the increment and the third number is the ending value (you may omit the increment if the increment you need is 1), or • using the linspace function to create a fixed number of values between two limits, e.g. C = linspace(0, 20, 11) where the first parameter is the lower limit, the second parameter is the upper limit, and the third parameter is the number of values in the vector. In the Command Window, enter the following: >> A = [2 5 7 1 3] A = 2 5 7 1 3 >> B = 1:3:20 B = 1 4 7 10 13 16 19 >> C = linspace(0, 20, 11) C = 0 2 4 6 8 10 12 14 16 18 20 >> D = [4] D = 4 Now, open the Workspace tab in the upper left Matlab window. It gives you three pieces of information about each of the variables you created: the name, value and “class” which for now, we can equate to “data type.” Notice that if the size of the vector is small enough, the value field shows its actual contents. Otherwise, you see a description of its attributes: <1 x 11 double> Table 1 - Constructing Arrays In the exercise above, we deliberately created the vector D with only one element, and the effect was perhaps surprising. D was presented in both the command window and the workspace window as if it were a scalar quantity. In fact, this is generally true in Matlab – all scalar quantities are really vectors of unit length. Concepts in Computer Science Implemented in Matlab Page 1-5 In the Command Window, enter the following: >> A = [2 5 7 1 3]; >> B = [0 6 5 3 2]; >> A >= 5 ans = 0 1 1 0 0 >> A >= B ans = 1 0 1 0 1 >> C = [1 2 3] >> A > C ??? Error using ==> gt Matrix dimensions must agree. Table 3 - Matrix Logical Operations 1.3.3. Concatenation In section 1.2.2 above, we saw that the first technique for creating a vector is to assemble numbers between square brackets: A = [2 5 7 1 3] This is in fact a special case of concatenation. Matlab permits the programmer to construct a new array by concatenating other arrays: A = [B C D … X Y Z] where the individual items within the brackets may be any array defined as a constant or variable. The only restriction is that all items within the brackets must have the same number of rows. The result will be an array with that number of rows and a number of columns equaling the sum of the number of columns in each individual item. The simple array constructor in section 1.2.2 above works because each number is a 1 x 1 array. The result is therefore a 1 x N array (vector) where N is the number of values on the brackets. In the Command Window, enter the following: >> A = [2 5 7]; >> B = [1 3]; >> [A B] ans = 2 5 7 1 3 Table 4 - Concatenating Arrays 1.3.4. Defining Ranges of Indices As we saw above, the basic operation of reading and writing the elements of a vector is called indexing. We will see below that indexing is not confined to single elements in a vector. We can accomplish indexing using vectors of indices. These index vectors may either be the values of previously named variables, or they can be created anonymously as they are needed. When we index a single element in a vector (e.g. A(4) ) we are actually creating an anonymous 1 x 1 index vector [4] and then using it to extract that number of elements from the array A. Page 1-6 Concepts in Computer Science Implemented in Matlab Creating anonymous index arrays as needed makes available some additional features of the colon (slice) operator. The general form of generating a vector of numbers is as follows: <start> : <increment> : <end> We already know that if we omit the <increment> portion, the default increment is 1. When used anonymously, the following features are also available: • The key word end is defined as the length of the vector, and • The operator ‘:’ by itself is short for 1:end. Finally, it is legal in Matlab to index with a vector of Boolean values of the same length as, or shorter than the vector being indexed. For example, if A is defined as: A = [2 5 7 1 3]; then A([false true false true]) returns ans = 5 1 This is extremely useful as we will see below for indexing items in a vector that match a specific Boolean test. 1.3.5. Slicing The general form of statements for moving sections of one array into sections of another is as follows: B(<range1>) = A(<range2>) Where <range1> and <range2> are vectors of indices, A is an existing array, and B can be an existing array, a new array, or absent altogether (giving B the name ans). The values in B at the indices in range1 are all assigned the values of A from range2. The rules for use of this template are as follows: • The size of range2 must be equal the size of range1. • If B did not exist before this statement was implemented, it is zero filled where assignments were not explicitly made. • If B did exist before this statement, the values not directly assigned in range1 remain unchanged. To obtain a grasp of these ideas, you should work carefully through the exercise below and study the explanatory notes that follow: Concepts in Computer Science Implemented in Matlab Page 1-7 Build and execute a script containing the following commands: clear % note 1 clc A = [2 5 7 1 3 4]; odds = 1:2:length(A); % note 2 % note 3 disp('odd values of A using predefined indices') A(odds) % note 4 disp('odd values of A using anonymous indices') A(1:2:end) % note 5 disp('put evens into odd values in a new array') B(odds) = A(2:2:end) % note 6 disp('set the even values in B to 99') B(2:2:end) = 99 % note 7 disp('find the small values in A') small = A < 4 % note 8 disp('add 10 to the small values') A(small) = A(small) + 10 % note 9 disp('this can be done in one ugly operation') A(A < 4) = A(A < 4) + 10 % note 10 You should see the following output in the Command Window: odd values of A using predefined indices ans = 2 7 3 odd values of A using anonymous indices ans = 2 7 3 put even values into odd values in a new array B = 5 0 1 0 4 set the even values in B to 99 B = 5 99 1 99 4 find the small values in A small = 1 0 0 1 1 0 add 10 to the small values A = 12 5 7 11 13 4 this can be done in one ugly operation A = 12 5 7 11 13 4 >> Table 5 - Vector Slicing Notes: 1. clear and clc should be the first two commands in a script (see Style Points below) 2. when predefining an index vector, if you want to refer to the size of a vector, you must use the length(…) function.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved