for
loops that traverse it and disp
lay the value and indeces on the command prompt% the details of what X exactly represents are not that important here ;]
h = [1 2 3 9];
X = convmtx(h,6)
Your nested for
loops should produce something like:
row=1, column=1, VALUE: 1
row=2, column=1, VALUE: 0
...
row=3, column=4, VALUE: 2
...
row=6, column=6, VALUE: 9
Step one is to think about how to loop over one dimension of this table... row 1, 2, .. - How big is ``X``? Hard-code the number of rows
% how big is X?
size(X) % 6 by 6
for iRow = 1:6
X(iRow, :) % this picks each row in turn
end
% but better not to hard-code the number 6
nRows = size(X,1);
for iRow = 1:nRows
X(iRow, :) % this picks each row in turn
end
Step two... the natural step is to try out the other way... we now need to put the index in the 2nd position!
nRows = size(X,1);
nColumns = size(X,2);
for iColumn = 1:nColumns
X(:, iColumn) % this picks each COLUMN in turn
end
Now, let's put those two things together. Read through the comments in the code and make sure you understand exactly what's happening. Draw a diagram of the matrix / table and indicate in which way the code steps through...
nRows = size(X,1);
nColumns = size(X,2);
% each time we go to a new row...
% we want then go through each column
for iRow = 1:nRows
% loop over rows (this loop runs 6 times)
% and each time we go to a new row (including the first)
% step through each column... that ends up being a single element
for iColumn = 1:nColumns
% this loop runs 6 times for each row
% so 6 x 6 times... 36
X(iRow, iColumn) % need to say which ROW and COLUMN
end
end
How can you traverse across first? What changes are required in your code?
If you only wanted to have one loop, can you think of a way to traverse the whole matrix?
Look up / google
Use if/else
to check the number of dimensions; the command ndims()
will help.
Processing images - loading and modifying images.