05a Arrays M0
-
Upload
jwinters20 -
Category
Documents
-
view
238 -
download
0
Transcript of 05a Arrays M0
-
8/3/2019 05a Arrays M0
1/28
COMP 1200Matlab Course Notes Set 5 - 1 of 59Auburn UniversityComputer Science and Software Engineering
Course Notes Set 5a:
COMP1200
Introduction to Computing for Engineers and ScientistsMATLAB Programming
Arrays: Vectors and Matrices
Computer Science and Software EngineeringAuburn University
-
8/3/2019 05a Arrays M0
2/28
COMP 1200Matlab Course Notes Set 5 - 2 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: IntroductionWhat if we had a problem where we needed to average all thegrades in a COMP1200 class? How would we store all thegrades entered by the user?
Using knowledge we have now we could approach this in acouple ways:
1. We could put all the grades in a data file. Then, open and readthe data file each time we want to work with the grades.Advantage: Code is relatively simple to write
Disadvantage: File I/O is very slow
2. We could declare a variable for each student in the class.
Wed then just write an input statement to load each variable.Advantage: All the variables are in memory, so its fast
Disadvantage: Thats a LOT of variables in a class with 100students!
-
8/3/2019 05a Arrays M0
3/28
COMP 1200Matlab Course Notes Set 5 - 3 of 59Auburn UniversityComputer Science and Software Engineering
% Read in grades in a variable for each gradeg1 = input(Enter 1st grade: );
g2 = input(Enter 2nd grade: );
. . .g100 = input(Enter 100th grade: );
% Average gradesavg = (g1 + g2 + g3 +...+ g100)/100.0;printf(Average is: %f,avg);
Lets imagine what a program using technique #2would look like:
-
8/3/2019 05a Arrays M0
4/28
COMP 1200Matlab Course Notes Set 5 - 4 of 59Auburn UniversityComputer Science and Software Engineering
Arrays
Now what happens when we add 20 students to the class? Wehave to add all those new variables! This is not good.
92 72 87 61 89 55 99 . 86 76
g1 g2 g3 g4 g5 g6 g7 . g99 g100
-
8/3/2019 05a Arrays M0
5/28
COMP 1200Matlab Course Notes Set 5 - 5 of 59Auburn UniversityComputer Science and Software Engineering
Arrays
A program with this many variables can be long and frustrating touse. What if there was a shorthand notation for the many relatedvariables. It would be nice if we could write:
%Read in grades
disp(Enter 100 grades: );
for i=1:100
gi = input(Enter a grade: );
This will not work because MATLAB would treat gi as a variable
and reassign 100 grades to it.
-
8/3/2019 05a Arrays M0
6/28
COMP 1200Matlab Course Notes Set 5 - 6 of 59Auburn UniversityComputer Science and Software Engineering
Arrays
This is where a MATLAB data structure called an arraycomes in tohelp us out.
You can visualize the compiler giving you 100 variables arranged in arow:
92 72 87 61 89 55 99 86 76
g1 g2 g3 g4 g5 g6 g7 g99 g100
g(1) g(2) g(3) g(4) g(5) g(6) g(7) g(99) g(100)
-
8/3/2019 05a Arrays M0
7/28COMP 1200Matlab Course Notes Set 5 - 7 of 59
Auburn UniversityComputer Science and Software Engineering
Array elementArray name Subscript
g(0)
We call each individual variable in an array an elementof thearray. We give the collection of elements a name, the array
name. An array can be given any valid variable name.
To get at the individual elements of an array, we number themstarting at one, incrementing by one. These numbersare called the subscripts.
-
8/3/2019 05a Arrays M0
8/28COMP 1200Matlab Course Notes Set 5 - 8 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Initializing with values>> t = [5 4 -2 1 3 9 7 0 -6] % Spaces or commas
t = 5 4 -2 1 3 9 7 0 -6
>> t(4)
ans =
1
>> t(3:2:9) % Colon Notation to access blocks
ans =
-2 3 7 -6
>> t(1:5) % Default step is 1
ans =5 4 -2 1 3
>> t(6:end)
ans =
9 7 0 -6
-
8/3/2019 05a Arrays M0
9/28COMP 1200Matlab Course Notes Set 5 - 9 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Initializing using expressions
>> x = [0 .1*pi .2*pi .3*pi .4*pi .5*pi]
x =0 0.3142 0.6283 0.9425 1.2566 1.5708
>> x = (0:.1:.5)*pi
x =
0 0.3142 0.6283 0.9425 1.2566 1.5708
>> x = x * 10
x =
0 3.1416 6.2832 9.4248 12.5664 15.7080
-
8/3/2019 05a Arrays M0
10/28COMP 1200Matlab Course Notes Set 5 - 10 of 59Auburn UniversityComputer Science and Software Engineering
Standard Arrays
>> ones(1,5)
ans =1 1 1 1 1
>> count=zeros(1,4)
count =
0 0 0 0
>> count(1)
ans =
0
>> par(1:5)=4
par =
4 4 4 4 4
-
8/3/2019 05a Arrays M0
11/28COMP 1200Matlab Course Notes Set 5 - 11 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Initializing
We can also initialize arrays from within our program, this is usually
done with a loop:
for k = 1:10
g(k) = k*0.5;
This program segment takes each element of an array and sets it tothe current value of k. After running, the array looks like:
0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
g(1) g(2) g(3) g(4) g(5) g(6) g(7) g(8) g(9) g(10)
-
8/3/2019 05a Arrays M0
12/28COMP 1200Matlab Course Notes Set 5 - 12 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Modifying
>> t = [5 4 -2 1 3 9 7 0 -6]
t =5 4 -2 1 3 9 7 0 -6
>> t(4) = 22
t =5 4 -2 22 3 9 7 0 -6
>> g = zeros(1,5)
g =
0 0 0 0 0
>> g = [10 20 30 40]
g =
10 20 30 40
-
8/3/2019 05a Arrays M0
13/28COMP 1200Matlab Course Notes Set 5 - 13 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Using In Programs
Using arrays in a program is no different than using normalvariables, you just have to remember the array subscript.
This is how the compiler will know which array element youwant to work with.
Lets take an example program that averages the elements of
an array:
-
8/3/2019 05a Arrays M0
14/28COMP 1200Matlab Course Notes Set 5 - 14 of 59Auburn UniversityComputer Science and Software Engineering
y = [76.0, 35.4, 99.2, 81.0,65.5];count = numel(y); % returns number of elements
sum = 0;
% Total each array elementfor n = 1:count
sum = sum + y(n);
end
% Compute the averageaverage = sum / count ;
fprintf('Average = %.2f\n',average);
76.0 35.4 99.2 81.0 65.5
y(1) y(2) y(3) y(4) y(5)
Arrays: Using In Programs
-
8/3/2019 05a Arrays M0
15/28COMP 1200Matlab Course Notes Set 5 - 15 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Using In Programs
Lets suppose we wanted to print all the values in the array:
for n = 1:count
fprintf(%.1f\n',y(n));
end
This prints each element of the array, one per line.
76.0
35.4
99.281.0
65.5
-
8/3/2019 05a Arrays M0
16/28COMP 1200Matlab Course Notes Set 5 - 16 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Using In Programs
Similarly, if we wanted to print the numbers on one line:
for n = 1:count
fprintf(%.1f ',y(n));
end% Add a next line after all numbers
fprintf(\n);
76.0 35.4 99.2 81.0 65.5
-
8/3/2019 05a Arrays M0
17/28COMP 1200Matlab Course Notes Set 5 - 17 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Using In Programs
Similarly, if we wanted to print them to a data file :
for n = 1:count
fprintf(myfile, '%.1f\n', y(n));
end
-
8/3/2019 05a Arrays M0
18/28COMP 1200Matlab Course Notes Set 5 - 18 of 59
Auburn UniversityComputer Science and Software Engineering
Arrays: Passing To Functions
We have to consider a couple cases when dealing witharrays and functions.
Passing A Single ElementPassing An Entire Array
-
8/3/2019 05a Arrays M0
19/28COMP 1200Matlab Course Notes Set 5 - 19 of 59
Auburn UniversityComputer Science and Software Engineering
Arrays: Passing A Single ElementTo Functions
function sum = sumFun( a, b )
... function bodyend
Now lets suppose in my main program I want to use sumFun,and I want it to operate on a couple of my array elements.
Heres how I would call the function:
x=[3 2 5 6 2]; i=4; j=1;result = sumFun(x(4), x(1));
or
result = sumFun(x(i), x(j));
So heres the rule: When the arguments are non-arrayvalues, the actual arguments sent must be subscripted arrayelements.
-
8/3/2019 05a Arrays M0
20/28COMP 1200Matlab Course Notes Set 5 - 20 of 59
Auburn UniversityComputer Science and Software Engineering
Arrays: Passing An Entire Array To Functions
When passing an entire array to a function, we usually have to
supply two parameters: the name of the array being passed andthe number of values that will be used.
Consider the following initialization:
g = [10, 20, 30, 40, 0, 0, 0, 0, 0, 0];
Memory will look like this:
10 20 30 40 0 0 0 0 0 0
g(1) g(2) g(3) g(4) g(5) g(6) g(7) g(8) g(9) g(10)
-
8/3/2019 05a Arrays M0
21/28COMP 1200Matlab Course Notes Set 5 - 21 of 59
Auburn UniversityComputer Science and Software Engineering
Arrays: Passing An Entire Array To Functions
Now only the first 4 elements of this 10 element array holdvalues that we care about. What we want to do is tell a
function to only consider those values when it operates on thearray.
Heres how we define the formal parameters for the function:
function sum = sumFun( x, n)...for j = 1:n
sum = sum + x(j);end
end
-
8/3/2019 05a Arrays M0
22/28COMP 1200Matlab Course Notes Set 5 - 22 of 59
Auburn UniversityComputer Science and Software Engineering
Arrays: Passing An Entire Array To Functions
To call the function,
function sum = sumFun( x, n)
we could do this:
g = [10, 20, 30, 40, 0, 0, 0, 0, 0, 0];
...
result = sumFun(g, 4); % NO subscript
-
8/3/2019 05a Arrays M0
23/28COMP 1200Matlab Course Notes Set 5 - 23 of 59
Auburn UniversityComputer Science and Software Engineering
[tempfile,msg] = fopen('temperature.txt','r');
if tempfile < 0 disp(msg);
else
numTemps = fscanf(tempfile, '%d',1);
% EOF determined by the count
for i = 1 : numTemps
temp(i) = fscanf(tempfile, '%f', 1);
end
fprintf('Maximum value: %.1f \n',
maximum(temp,numTemps));
fclose(tempfile);
end
This program reads values from a data file anddetermines the maximum value with a function.
-
8/3/2019 05a Arrays M0
24/28
COMP 1200Matlab Course Notes Set 5 - 24 of 59Auburn UniversityComputer Science and Software Engineering
[tempfile,msg] = fopen('temperature.txt','r');
if tempfile < 0 disp(msg);
else % NO blank line at end of values
numTemps = 0;
while ~feof(tempfile) % Looks for EOF
numTemps = numTemps + 1;
temp(numTemps) = fscanf(tempfile, '%f', 1);
end
fprintf('Maximum value: %.1f \n',
maximum(temp,numTemps));
fclose(tempfile);
end
This program reads values from a data file anddetermines the maximum value with a function.
-
8/3/2019 05a Arrays M0
25/28
COMP 1200Matlab Course Notes Set 5 - 25 of 59Auburn UniversityComputer Science and Software Engineering
% This function returns the maximum% value in the array x with n elements.
functionmax_x = maximum( x, n )
max_x = x(1);for k = 1:n
if x(k) > max_x
max_x = x(k);
endend
end
-
8/3/2019 05a Arrays M0
26/28
COMP 1200Matlab Course Notes Set 5 - 26 of 59Auburn UniversityComputer Science and Software Engineering
Arrays: Passing An Entire Array To Functions
maxTemp = maximum( temp,numTemps )
functionmax_x = maximum( x, n)
fprintf('Max temp: %.1f \n',
maximum(temp,numTemps));
-
8/3/2019 05a Arrays M0
27/28
COMP 1200Matlab Course Notes Set 5 - 27 of 59Auburn UniversityComputer Science and Software Engineering
The load command reads values
Saved with the save command Saved in text (.txt)
All at once and assigns to the given variable name
clc, clear all
temp = load('temperature.txt');
fprintf('Maximum value: %.1f \n',
maximum(temp, numel(temp)));
Reading all values using load()
-
8/3/2019 05a Arrays M0
28/28
Auburn University
To be continued