Week : For For If End End End End Function
Week : For For If End End End End Function
Our plagiarism check found students who had very similar code:
Programming in MATLAB
Y
Make sure to write your code yourself!
/
Introduction Introduction
Recap Last Week Todays Topics
/ /
/ 6/
Coding Tips Coding Tips
Over-commenting Overloading builtin
There is a point where too many comments reduce the Spotted this type of code a few times in Sum Divisors:
readability of your code: sum = 0;
for i=1:n
function [S] = my_sum(x) sum = sum + i;
% MY_SUM Sum numbers in a vector end
% [S] = my_sum(x) sums all elements in x
/ 8/
/ /
/ /
Matrix Algebra Matrix Algebra
Using Matrices: Transpose Intermezzo: numel, length & size
Transposing a matrix in MATLAB can be done using the There is a difference between numel, length, and size:
apostrophe:
>> X = [1 2 3; 4 5 6]
>> Y = X';
>> X = [1 2 3; 4 5 6]
>> size(X)
1 2 3
2 3
4 5 6
>> size(Y)
>> Y = X'
3 2
1 4
>> length(X)
2 5
3
3 6
>> length(Y)
3
Get matrix dimensions using size: >> numel(X)
6
>> size(X) >> numel(Y)
ans = 2 3 6
>> size(Y) >> size(X, 1)
ans = 3 2 2
/ /
/ 6/
Matrices can be indexed using a range too: Some more matrix indexing:
Logical vectors can be combined with a single | or &: Why is there even a difference between |, ||, &, and &&?
>> x = 5:10
5 6 7 8 9 10 Here, both sides are evaluated, and then & is performed:
>> x < 7 x = 0;
1 1 0 0 0 0 if (x ~= 0) & (5/x < 1.0)
a = a + x;
>> x > 8 end
0 0 0 0 1 1
Whereas here, evaluation stops after the rst comparison:
>> x < 7 | x > 8 % use | (OR) to combine
1 1 0 0 1 1 x = 0;
if (x ~= 0) && (5/x < 1.0)
>> x(x < 7 | x > 8) a = a + x;
5 6 9 10 end
>> x(x > 7 & x < 10) % use & (AND) to combine Therefore, we use || and && in if statements, and | and & with
8 9 indexing.
/ /
Plotting Plotting
Demo: Plotting Demo: Plotting: Intro
>> y = 1:10;
Plotting functionality in MATLAB is great! >> plot(y)
>> x = 5:5;
>> y = x .^ 2
Note: The demo le will be on Blackboard. The following slides >> plot(x, y)
are just for reference.
Closing all plots:
>> close
/ /
Plotting Plotting
Demo: Plotting: Options Demo: Plotting: Labelling
/ 6/
Plotting Plotting
Demo: Plotting: Combining plots -D Plotting: Intro
150
50
0 5
-10 -10
/ 8/
Plotting Plotting
-D Plotting: Meshgrid -D Plotting: Meshgrid
/ /
Plotting File I/O
Utilities The Workspace
or:
30000 >> save('myworkspace.mat', 'x', 'y')
>> load('myworkspace.mat')
10000
0
-6 -4 -2 0 2 4 6
/ /
Real-life data is often supplied as CSV les, which look like this:
Writing data to screen can be done using fprintf
25.133,3.1416,18.85
9.4248,15.708,21.991 >> fprintf('Bananas contain a lot of potassium.')
12.566,28.274,6.2832 Bananas contain a lot of potassium.>>
We can load this data into a matrix using Notice that there is no linebreak after the sentence, we have to
add this manually:
>> A = csvread('mydata.csv');
>> fprintf('Bananas contain a lot of potassium.\n')
Writing data to CSV les is not done using csvwrite but through: Bananas contain a lot of potassium.
>>
>> dlmwrite('myoutput.csv', A, 'precision', 16);
/ /
The function fprintf can also format numbers or strings into Writing formatted text to a le requires opening the le in
text: MATLAB rst:
>> fprintf('%i bananas have %.2f grams of %s.\n', ... >> fileID = fopen('myoutputfile.txt', 'w');
3, 1.266, 'potassium')
3 bananas contain 1.27 grams of potassium. Then, we can write to the le by specifying the le ID in fprintf:
>>
>> fprintf(fileID, 'Bananas!\n');
Here, we have format strings: >> fprintf(fileID, ...
'%i banana contains %.3f grams of potassium.\n', ...
I %i prints an integer 1, 0.422);
I %.2f prints a oating point number to decimal places
I %s prints a string When were done writing to the le, we close it again:
>> fclose(fileID);
Format strings are available in many other languages, including
Java. Note: Not closing the le can lead to weird bugs!
/ 6/
Memory Usage Memory Usage
Theory Practice
>> x = 1:3;
Memory in your computer (RAM) can be seen as a list of boxes:
itll be in adjacent boxes somewhere:
... ...
... ...
>> x = [x 4];
Programs that constantly have to ask the OS for more boxes, will
be slow. Which means a reallocation occurs:
... ...
Proling Proling
Demo: Timing Code Sections Demo: Proling
tic; If you want to nd out where your code is slow, use the proler:
x = 1:1e5;
y = []; >> profile on
for xi=x >> my_slow_function();
y = [y xi^2]; >> profile off
6 end >> profile viewer
elapsed = toc
tic;
x = 1:1e5;
First make it work, then make it fast.
y = x.^2;
elapsed = toc
/ /