IE142 Week 14
IE142 Week 14
2021–2022 Spring
Week 14 (24.05.2022)
• Although the word "optimization" shares the same root as "optimal", it is rare for the process of
optimization to produce a truly optimal system. A system can generally be made optimal not in
absolute terms, but only with respect to a given quality metric, which may be in contrast with other
possible metrics. As a result, the optimized system will typically only be optimal in one application or
for one audience. One might reduce the amount of time that a program takes to perform some task
at the price of making it consume more memory. In an application where memory space is at a
premium, one might deliberately choose a slower algorithm in order to use less memory. Often there
is no "one size fits all" design which works well in all cases, so engineers make trade-offs to
optimize the attributes of greatest interest.
• Optimization can reduce readability and add code that is used only to improve the performance.
This may complicate programs or systems, making them harder to maintain and debug. As a result,
optimization or performance tuning is often performed at the end of the development stage.
IE142 Computer Programming II Week 12 (10.05.2022)
Exercises
Write the code for function fun_gcd(A,B) that will output the “greatest
common divisor” of positive integers A and B.
IE142 Computer Programming II Week 12 (10.05.2022)
Exercises
Write the code for function fun_gcd(A,B) that will output the “greatest
common divisor” of positive integers A and B.
Solution:
1st version: «optimized» version:
Exercises
Write the code for function fun_primes(A) that will output an array
containing all prime numbers smaller than or equal to the positive integer A.
IE142 Computer Programming II Week 12 (10.05.2022)
Exercises
Write the code for function fun_primes(A) that will output an array
containing all prime numbers smaller than or equal to the positive integer A.
Solution:
1st version: «optimized» version:
A = fun_primes(300000);
Elapsed time is 1.147309 seconds..
IE142 Computer Programming II Week 14 (24.05.2021)
The format string '%f %c' specifies that on each line there is a double value followed by a
space followed by a character. This creates a 1 x 2 cell array variable called subjdata. The
first element in this cell array is a column vector of doubles (the first column from the file);
the second element is a column vector of characters (the second column from the file), as
shown here:
>> subjdata
subjdata =
[5x1 double] [5x1 char]
>> subjdata{1}
ans =
5.3000
2.2000
3.3000
4.4000
1.1000
>> subjdata{2}
ans = To refer to individual values from the vector, it is necessary to index into the cell array
a using curly braces and then index into the vector using parentheses. For example, to refer
b to the third number in the first element of the cell array:
a
a >> subjdata{1}(3)
ans = 3.3000
b
IE142 Computer Programming II Week 14 (24.05.2021)
Create a text file using a text editor (e.g., Notepad) in your computer, name it Products.txt,
and enter the following lines into this file as follows:
Products.txt
1001 hammer 2.58
1002 plier 1.20
1003 screwdriver 1.56
1004 soldering iron 3.70
1005 wrench 2.60
Save the file, write the following Octave code;
f1=fopen('Products.txt');
C = textscan(f1,'%d %s %f');
fclose(f1);
Suppose now that the file SysDia.txt contains the patients’ names as well as their blood
pressure data, and that we wish to read in and store all of the data, i.e. names and blood
pressures:
Joe Bloggs 51 85
Josephine Bloggs 88 141
Zhang San 67 95
Anders Andersen 77 111
Erika Mustermann 68 115
Ola Nordmann 99 171
Jan Kowalski 80 121
Since, textscan returns a cell array rather than a normal array, and cell arrays can contain
mixed data types, it is possible to read this data using textscan.
% open file
fid = fopen('SysDia.txt', 'r');
if (fid == −1)
error('File cannot be opened');
end
% close file
fclose(fid);
The type of the variable data returned by textscan will now be a 1×4 cell array.
The first and second elements of data are arrays that contain the first names and
second names of the patients respectively. In fact, since the lengths of the strings vary
these first two elements are themselves cell arrays.
a. The third and fourth elements of data are normal arrays of integers representing
the systolic and diastolic blood pressures of the patients. Try entering this code into
an Octave script m-file, running it and inspecting the data variable.
IE142 Computer Programming II Week 14 (24.05.2021)
readtable
Create table from file
Syntax
T = readtable(filename)
T = readtable(filename,Name,Value)
T = readtable(filename,opts)
T = readtable(filename,opts,Name,Value)
Description
T = readtable(filename,Name,Value) creates a table from a file with additional options specified by one or more name-value pair
arguments.
For example, you can specify whether readtable reads the first row of the file as variable names or as data.
Create a table using data from a specified region of the spreadsheet patients.xls. Use the data
from the 5-by-3 rectangular region between the corners C2 and E6. Do not use the first row of
this region as variable names.
T = readtable('Ex_Excel_Book1.xlsx', 'Range', 'A2:C4', 'ReadVariableNames',false)
T contains default variable names.
Create a table from a spreadsheet that contains variable names in the first row and row names
in the first column.
T = readtable('Ex_Excel_Book1.xlsx','ReadRowNames',true);
IE142 Computer Programming II Week 14 (24.05.2021)
IMAGE FILES
Color images are represented as matrices, of picture elements called pixels.
In MATLAB, an image is represented by a matrix in which each element corresponds to a
pixel in the image. Each element that represents a particular pixel stores the color for that
pixel.
There are two basic ways in which the color can be represented:
true color or RGB, in which the three color components are stored (red, green, and
blue, in that order) in layers in a three-dimensional matrix,
index into a colormap, in which the value stored for each pixel is an integer that refers
to a row in another matrix called a colormap; the colormap stores the red, green, and
blue components of colors in three separate columns.
Thus, for an image that has m n pixels, there are two methods for representing color. In
the true color or RGB method, all of the information is stored in one 3D matrix with the
size m n 3. The first two indices represent the coordinates of the pixel. The third index
is the color component, so (:,:,1) is the red, (:,:,2) is the green, and (:,:,3) is the blue
component.
Typically, colormaps use double values in the range from 0 to 1, and RGB matrices use the
type uint8. We will use only RGB matrices here.
IE142 Computer Programming II Week 14 (24.05.2021)
In an 8-bit RGB image, each element in the matrix is of the type uint8, which is an unsigned
integer type storing values in the range from 0 to 255.
The minimum value, 0, represents the darkest hue available, so all 0s result in a black
pixel. The maximum value, 255, represents the brightest hue.
For example, if the values for a given pixel coordinates px and py are: (px,py,1) is 255,
(px, py,2) is 0, and (px,py,3) is 0, then that pixel will be bright red.
All 255s result in a white pixel.
Images that are stored in various formats, such as JPEG, TIFF, PNG, GIF, and BMP, can be
manipulated in Octave. Built-in functions, such as imread and imwrite, read from and write
to various image file formats. Some images are stored as unsigned 8-bit data (uint8), some
as unsigned 16-bit (uint16), and some are stored as double.
For example, the following reads a JPEG image into a 3D matrix; it can be seen from the
size and class functions that this was stored as a uint8 RGB matrix.
>> img = imread('a.jpg') ;
>> size(img)
ans =
467 350 3
>> class(img)
ans = uint8
The image is stored as a true color matrix and has 467350 pixels. The image function
displays the matrix as an image, as shown in.
>> img = imread('a.jpg') ;
>> size(img)
ans =
467 350 3
>> class(img)
ans = uint8
>> class(img)
IE142 Computer Programming II
ans = uint8
Week 14 (24.05.2021)
The image is stored as a true color matrix and has 467350 pixels. The image function
displays the matrix as an image.
img1 = imread('a.jpg');
img2 = img1 ; img2(:,:,1) = 0 ;
img3 = img1 ; img3(:,:,2) = 0 ;
img4 = img1 ; img4(:,:,3) = 0 ;
subplot(2,2,1), imshow(uint8(img1));
subplot(2,2,2), imshow(uint8(img2)); % only the R component is set to zero
subplot(2,2,3), imshow(uint8(img3)); % only the G component is set to zero
subplot(2,2,4), imshow(uint8(img4)); % only the B component is set to zero
IE142 Computer Programming II Week 14 (24.05.2021)