To fulfill this assignment, you need to submit these files (all packed in one .zip
file) into the upload system:
answers.txt
- answers to the Assignment Questions
assignment_basics.m
- a script for data initialization, calling of the implemented functions and plotting of their results (for your convenience, will not be checked)
matrix_manip.m
- a function implementing the matrix manipulation tasks specified in the section Matrix manipulation
compute_letter_mean.m
and compute_lr_histogram.m
- functions specified in the section Simple data task
initial1_mean.png
, initial2_mean.png
and initials_histograms.png
- images specified in the section Simple data task
Use template of the assignment. When preparing a zip file for the upload system, do not include any directories, the files have to be in the zip file root.
Beware of using for
loops! :)
Follow the instructions on https://www.fel.cvut.cz/user-info/matlab.html.
Statistical Pattern Recognition Toolbox contains variety of algorithms which you will implement by yourself during this course (you can use them as an inspiration) and contains also many support functions which we will use throughout the semester.
Download the latest version of the toolbox from https://gitlab.fel.cvut.cz/smidm/statistical-pattern-recognition-toolbox. Unpack the content of the .zip
file into your working directory (you should be able to find it next week!). To be able to use the toolbox, you have to set the path to it:
stprpath
will set the path and do initialization of the toolbox
Now you can switch to any other directory and still use the functions from the STPR toolbox.
To verify the paths try to run demo_ocr
.
We will be using MATLAB programming language during the whole semester. It is necessary for you to be as familiar with it as possible, since the labs are time limited and the tasks are often challenging. It may happen that you spend more time looking for MATLAB syntax of some function instead of solving the problem itself.
For the case you are not too sure about your MATLAB skills, here are few useful links:
In most of the assignments there will be a set of simple questions. Answering the questions should help you to solve the assignment. All questions need to be answered correctly to get the points for the assignment.
Fill the correct answers to your answers.txt
file.
Example answers.txt
(bogus answers only, fill the correct ones):
question1 : gen_zero_matrix question2 : 'x*' # enclose special characters in tick marks, also known as single quotation marks. question3 : '^' # enclose special characters in tick marks, also known as single quotation marks. question4 : [a,d,e] question5 : [b,f]
In the first part of today’s assignment, you will start with some simple matrix manimulation tasks.
AVOID THE USE OF ANY LOOPS IN YOUR PROGRAM!
Your goal is to complete a function output = matrix_manip(A, B)
, where A
and B
are input matrices and output
is a resulting structure containing results of operations described below.
To have some data to work with, use matrices A
and B
from assignment_basics.m
:
A = [16 2 3 13; 5 11 10 8; 9 7 6 12; 4 14 15 1] B = [ 3 4 9 4 3 6 6 2 3 4; 9 2 10 1 4 3 7 1 3 5]
Function should work on general input matrices, not only for above prescribed A
and B
matrices or matrices with the same dimensions. Dimensions of the input matrices will be always suitable for all of the following tasks.
A
and return it in output.A_transpose
. Example result: >> output.A_transpose output.A_transpose = 16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1Hint: Search documentation with lookfor command.
A
and return it in output.A_3rd_col
.>> output.A_3rd_col output.A_3rd_col = 3 10 6 15Hint: You need to use matrix indexing with colon
:
character.
output.A_slice
. >> output.A_slice output.A_slice = 7 6 12 14 15 1
A
greater then 3 and increment them by 1. Afterwards add column of ones to the matrix. Save the result to output.A_gr_inc
. >> output.A_gr_inc output.A_gr_inc = 17 2 3 14 1 6 12 11 9 1 10 8 7 13 1 5 15 16 1 1Hint: Try
>
operator on the whole matrix. Use []
to construct the new matrix and ones function to create a vector with ones.
C
such that $C_{i,j} = \sum_{k=1}^n A\_gr\_inc_{i,k} \cdot (A\_gr\_inc^T)_{k,j}$ and store it in output.C
. >> output.C output.C = 499 286 390 178 286 383 351 396 390 351 383 296 178 396 296 508Hint: No loops are needed, try it on a paper with a 2×2 matrix.
>> output.A_weighted_col_sum output.A_weighted_col_sum = 391Hint: You will need colon : special character to generate ranges, per element array multiplication
.*
and sum()
function.
B
. Save the result to matrix output.D
.>> output.D output.D = -1 0 5 0 -1 2 2 -2 -1 0 3 -4 4 -5 -2 -3 1 -5 -3 -1Hint: Use
repmat
command to replicate a matrix .
D
, which have greater euclidean distance than the average euclidean distance.>> output.D_select output.D_select = 0 5 0 -2 -4 4 -5 -5Hint: You can find useful element-wise power of two
.^2
and find
function that returns indices of non zero elements of a matrix. Euclidean length of a vector can be calculated with the norm
function, but for calculating length of all column vectors in a matrix you need to do it with your own code. No loops are needed!
In this part of the assignment, you are supposed to work with a simple input data which contains images of letters. We will use similar data structures later on during the labs. Do the following:
Note: Plotting and displaying of images should be implemented only in assignment_basics.m
.
data_33rpz_cv01.mat
data file, the following variables are stored:
images
(3D array of 2000 10×10 grayscale images)
Alphabet
(letters in the images
, not full alphabet is included)
labels
(indexes of the images
into Alphabet
array).
initial1_mean.png
and initial2_mean.png
(use any letter if any of your initials is not present in the dataset). You will probably encounter a problem with non-decimal numbers in the image matrix. You can avoid it by using the uint8
function before returning the mean image.
letter_mean = compute_letter_mean(letter_char, Alphabet, images, labels)where
letter_char
is a character (e.g. 'A', 'B', 'C') representing the letter whose mean we want to compute, Alphabet
, images
and labels
are loaded from the provided data, and letter_mean
is the resulting mean image.
x = sum of pixel values in the left half of image - sum of pixel values in the right half of imageThen make a histogram of feature values of all images of a letter. Complete a function for the feature histogram computation:
lr_histogram = compute_lr_histogram(letter_char, Alphabet, images, labels, num_bins)where
letter_char
is a character representing the letter whose feature histogram we want to compute, Alphabet
, images
and labels
are loaded from the provided data, num_bins
is the number of histogram bins and lr_histogram
is the resulting histogram (num_bins
long vector containing counts of items in the corresponding bins).
>> compute_lr_histogram('A', Alphabet, images, labels, 10) ans = 1 1 3 6 12 27 24 20 5 1
hist
function to compute the histogram.
initials_histograms.png
.