Could somebody explain the following code snippet? I have no background in computer science or programming and just recently became aware of Matlab. I understand the preallocation part from data=ceil(rand(7,5)*10)...
to ...N*(N-1)/2)
.
I need to understand every aspect of how matlab processes the code from kk=0
to the end. Also, the reasons why the code is codified in that manner. There's no need to explain the function of: bsxfun(@minus)
, just how it operates in the scheme of the code.
data=ceil(rand(7,5)*10);
N = size(data,2);
b=cell(N-1,1);
c=NaN(size(data,1),N*(N-1)/2);
kk=0;
for ii=1:N-1
b{ii} = bsxfun(@minus,data(:,ii),data(:,ii+1:end));
c(:,kk+(1:N-ii)) = bsxfun(@minus,data(:,ii),data(:,ii+1:end));
kk=kk+N-ii;
end
Start at zero
Loop with ii going from 1 up to N-1 incrementing by 1 every iteration. Type
1:10
in the command line of matlab and you'll see that it outputs1 2 3 4 5 6 7 8 9 10
. Thuis colon operator is a very important operator to understand in matlab.b{ii} = ... this just stores a matrix in the next element of the cell vector b. Cell arrays can hold anything in each of their elements, this is necessary as in this case each iteration is creating a matrix with one fewer column than the previous iteration. data(:,ii) --> just get the iith column of the matrix data (: means get all the rows) data(:, ii + 1:end) means get a subset of the matrix data consisting of all the rows but only of columns that appear after column ii bsxfun(@minus, data(:,ii), data(:,ii+1:end)) --> for each column in the matrix data(:, ii+1:end), subtract the single column data(:,ii)
%This does the same thing as the line above but instead of storing the resulting matrix of the loop in a separate cell of a cell array, this is appending the original array with the new matrix. Note that the new matrix will have the same number of rows each time but one fewer column, so this appends as new columns. %c(:,kk + (1:N-ii)) = .... --> So 1:(N-ii) produces the numbers 1 up to the number of columns in the result of this iteration. In matlab, you can index an array using another array. So for example try this in the command line of matlab:
a = [0 0 0 0 0]; a([1 3 5]) = 1
. The result you should see isa = 1 0 1 0 1
. but you can also extend a matrix like this so for example now typea(6) = 2
. The result:a = 1 0 1 0 1 2
. So by using c(:, 1:N-ii) we are indexing all the rows of c and also the right number of columns (in order). Adding the kk is just offsetting it so that we do not overwrite our previous results.Now we just increment kk by the number of new columns we added so that in the next iteration, c is appended at the end.
I suggest that you put a breakpoint in this code and step through it line by line and look at how the variables change in matlab. To do this click on the little dashed line next to
k=0;
in the mfile, you will see a red dot appear there, and then run the code. The code will only execute as far as the dot, you are now in debug mode. If you hover over a variable in debug mode matlab will show its contents in a tool tip. For a really big variable check it out in the workspace. Now step through the code line by line and use my explanations above to make sure you understand how each line is changing each variable. For more complex lines likeb{ii} = bsxfun(@minus,data(:,ii),data(:,ii+1:end));
you should highlight code snippets and ruin these in the command line to see what each part is doing so for example rundata(:,ii)
to see what that does and then trydata(:,ii+1:end))
or even justii+1:end
(well in that case it wont work, replace end with size(data, 2)). Debugging is the best way to understand code that confuses you.