Before the lab
- Study for the Python quiz
- Read
In Octave we can multiply every element of a matrix by a scalar using the .* operator
A=[1,2,3;
4,5,6];
B=A.*2
In general .* supports any two arguments of the same size.
C=A .* [2,2,2; 2,2,2]
It turns out these are actually the same operation, since Octave converts the first into the second via broadcasting
Quoting from the Octave docs, for element-wise binary operators and functions
The rule is that corresponding array dimensions must either be equal, or one of them must be 1.
In the case where one if the dimensions is 1
, the smaller matrix is
tiled to match the dimensions of the larger matrix.
Here's another example you can try.
x = [1 2 3;
4 5 6;
7 8 9];
y = [10 20 30];
x + y
One potentially surprising aspect of Octave arrays is that the number
of dimensions is independent from the number of elements. We can add
as many dimensions as we like, as long as the only possible index in
those dimensions is 1
. This can be particularly useful when trying
to broadcast with higher dimensional arrays.
ones(3,3,3) .* reshape([1,2,3],[1,1,3])
ones(3,3,3) .* reshape([1,2,3],[1,3,1])
Complete the following function. You may want to copy the definitions of A
and B
into the REPL
to understand the use of cat
.
## usage: scale_layers(array, weights)
##
## multiply each layer of a 3D array by the corresponding weight
function out = scale_layers(array, weights)
out =
endfunction
%!test
%! onez = ones(3,3);
%! A=cat(3,onez, 2*onez, 3*onez);
%! B=cat(3,onez, 6*onez, 15*onez);
%! assert(scale_layers(A,[1;3;5]),B)
Save the image above left as
~/cs2613/labs/L21/paris.jpg
(make sure you get the
full resolution image, and not the thumbnail).
Run the following demo code; you can change the weight vector for different colourization.
paris=imread("paris.jpg");
sepia=scale_layers(paris,[0.9,0.62,0.34]);
imshow(sepia);
You should get something like the following
The second half of the lab will be a quiz on Python.
Read