Professional Documents
Culture Documents
(CSC209)
Lecture (3)
Multidimensional Arrays and
Displaying Output Data
Dr.Omar Almutairi
Multidimensional Arrays
2
Accessing Multidimensional Arrays
with One Dimension
3
Subarrays
4
The end Function
One raw
5
Using Subarrays on the Left-Hand Side
of an Assignment Statement
6
Assigning a Scalar to a Subarray
arr4 = [1 2 3 4; 5 6 7 8; 9 10 11 12];
» arr4(1:2,1:2) = 1
arr4 =
1 1 3 4
1 1 7 8
9 10 11 12
7
Sub-arrays
Delete Number
8
Special Values
9
Displaying Output Data
11
The display function
Example 1
>> str = ['The value of pi = ' num2str(pi)];
disp(str);
The value of pi = 3.1416
Example 2
x = 1:10;
y = x.^2 + 2;
z = sqrt(x);
disp('Table using disp command’), disp('x y z');
disp([x', y', z'])
12
Data Files
• Two types
Save Data (.mat file)
Load (call .mat file)
13
Fprintf function
fprintf(format,data)
fprintf('The value of pi is %f \n',pi)
14
Fprintf function Example
The fprintf statement provides control the
way that numeric and string data are printed
to the command window or a file.
Example
>> A1 = [9.9, 9900];
>> A2 = [8.8, 7.7; 8800, 7700];
>> formatspec = 'X is %4.2f meters or %8.3f
mm\n';
>> fprintf(formatspec,A1,A2)
X is 9.90 meters or 9900.000 mm
X is 8.80 meters or 8800.000 mm
X is 7.70 meters or 7700.000 mm 15
Practice & Quiz 3
16
END
17