12p. Intro to arrays _printable_

# 12p. Intro to arrays _printable_ - Intro to Arrays in...

This preview shows pages 1–3. Sign up to view the full content.

1 ©2009 by L. Lagerstrom Intro to Arrays in Matlab • For an introduction to the basic concepts, see the “Intro to arrays” video clip • Three ways to create row vectors • Matlab’s addressing system • Creating column vectors • Creating and addressing 2D arrays ©2009 by L. Lagerstrom Note re. Code and Display In the code examples that follow, we will often have the results displayed simply by leaving off the semi-colon at the end of assignment statements. (The results will be displayed in a simulated Command window.) In addition, we will assume that the display format has been set to “format bank” (i.e., results displayed to 2 decimal places), simply to save space. Finally, the display of the results will have blank lines in it for readability, but the code that would create those blank lines (e.g., disp(‘ ’) ) will not be shown in the code examples (again, for readability’s sake). ©2009 by L. Lagerstrom Three Ways to Create Row Vectors • By explicitly listing the elements • By using “colon notation” • By using the linspace or logspace functions Matlab code Command window display ©2009 by L. Lagerstrom 1. Row Vectors by Explicit List %A. List the values (or "elements") %within square brackets. (Leave %off semi-colon to display results.) x = [3 -45.2 320 78] %B. Okay to use commas to separate %the elements (or commas and spaces) x = [3,-45.2,320,78] %C. Okay to use math expressions as %elements y = pi/6; z = 4; x = [56 3/z+2 4*sin(y)] %D. Row vectors may themselves be %used as elements in the creation %of new vectors x1 = [4 9 2]; x2 = [-3 8]; x3 = [x2 54.2 x2 x1] %E. Square brackets required x4 = (4 9 2); %Gives error message x = 3.00 -45.20 320.00 78.00 x = 3.00 -45.20 320.00 78.00 x = 56.00 2.75 2.00 x3 = Columns 1 through 4 -3.00 8.00 54.20 -3.00 Columns 5 through 8 8.00 4.00 9.00 2.00

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Matlab code Command window display ©2009 by L. Lagerstrom 2. Row Vectors via Colon Notation %A. Use "colon notation" to specify %beginning and ending values for the %elements in the row vector. The %increment will be 1. Note that %square brackets not necessary (but %okay, as are parentheses). x1 = 1:5 x2 = 3:7 x3 = [3:7] x4 = (3:7) %B. We can specify the increment %by adding a middle value. x5 = 1:1:5 x6 = 7:2:13 x7 = 7:2:14 x8 = 5:-1:2 %Counting down, 5 to 2 x9 = 0:0.25:1 %Non-integers okay x10 = 2.3:0.2:3.1 x1 = 1.00 2.00 3.00 4.00 5.00 x2 = 3.00 4.00 5.00 6.00 7.00 x3 = 3.00 4.00 5.00 6.00 7.00 x4 = 3.00 4.00 5.00 6.00 7.00 x5 = 1.00 2.00 3.00 4.00 5.00 x6 = 7.00 9.00 11.00 13.00 x7 = 7.00 9.00 11.00 13.00 x8 = 5.00 4.00 3.00 2.00 x9 = 0 0.25 0.50 0.75 1.00 x10 = 2.30 2.50 2.70 2.90 3.10 Matlab code Command window display ©2009 by L. Lagerstrom 3. Row Vectors via linspace and logspace %A. The linspace function generates %linearly (equally) spaced values. x1 = linspace(1,5,5)
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/23/2010 for the course ENG 42325 taught by Professor Lagerstrom during the Spring '10 term at UC Davis.

### Page1 / 7

12p. Intro to arrays _printable_ - Intro to Arrays in...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online