Академический Документы
Профессиональный Документы
Культура Документы
Arrays: Introduction
What if we had a problem where we needed to average all the grades in a COMP1200 class? How would we store all the grades entered by the user? Using knowledge we have now we could approach this in a couple ways:
1.
We could put all the grades in a data file. Then, open and read the data file each time we want to work with the grades.
Advantage: Disadvantage: Code is relatively simple to write 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: Disadvantage: All the variables are in memory, so its fast Thats a LOT of variables in a class with 100 students!
Arrays
Now what happens when we add 20 students to the class? We have to add all those new variables! This is not good.
92 g1
72 g2
87 g3
61 g4
89 g5
55 g6
99 g7
. .
86
76
g99 g100
Arrays
A program with this many variables can be long and frustrating to use. What if there was a shorthand notation for the many related variables. 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.
Arrays
This is where a MATLAB data structure called an array comes in to help us out. You can visualize the compiler giving you 100 variables arranged in a row:
92
g1
72
g2
87
g3
61
g4
89
g5
55
g6
99
g7
86
g99
76
g100
Array element
Array name Subscript
g(0)
We call each individual variable in an array an element of the array. 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 them starting at one, incrementing by one. These numbers are called the subscripts.
1.5708
0.9425
1.2566
1.5708
>> x = x * 10 x = 0 3.1416
6.2832
9.4248
12.5664
15.7080
Standard Arrays
>> ones(1,5) ans = 1 1
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 to the current value of k. After running, the array looks like:
0.5
g(1)
1.0
g(2)
1.5
g(3)
2.0
g(4)
2.5
g(5)
3.0
g(6)
3.5
g(7)
4.0
g(8)
4.5
g(9)
5.0
g(10)
Arrays: Modifying
>> t = [5 4 -2 1 3 9 7 0 -6] t = 5 4 -2 1 3 9 7 >> t(4) = 22 t = 5 4 -2
-6
22
-6
% Total each array element for n = 1:count sum = sum + y(n); 76.0 35.4 99.2 81.0 65.5 end y(1) y(2) y(3) y(4) y(5) % Compute the average average = sum / count ; fprintf('Average = %.2f\n',average);
We have to consider a couple cases when dealing with arrays and functions. Passing A Single Element Passing An Entire Array
or
result = sumFun(x(i), x(j)); So heres the rule: When the arguments are non-array values, the actual arguments sent must be subscripted array elements.
Auburn University Computer Science and Software Engineering
g(1) g(2) g(3) g(4) g(5) g(6) g(7) g(8) g(9) g(10)
we could do this:
g = [10, 20, 30, 40, 0, 0, 0, 0, 0, 0]; ... result = sumFun(g, 4); % NO subscript
[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 and determines the maximum value with a function.
Auburn University Computer 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 and determines the maximum value with a function.
Auburn University Computer Science and Software Engineering
% This function returns the maximum % value in the array x with n elements. function max_x = maximum( x, n ) max_x = x(1); for k = 1:n if x(k) > max_x max_x = x(k); end end end
clc, clear all temp = load('temperature.txt'); fprintf('Maximum value: %.1f \n', maximum(temp, numel(temp)));
To be continued