No-one should get the wrong idea here, Dragon is a much better coder than me. Matlab is a language that allows people like me with no real computing background (I haven't done any computer courses) to avoid a lot of low-level stuff.
I think that's all I use it for here, but linspace is more general; linspace(a,b,n) creates a 1-by-n vector
Just to be sure to be sure. This creates a row right, not a column?
Yep, a row.
Would linspace(a;b,n) have create a column or am I trying to be clever?
That's a brilliant inference, but semicolons don't mean anything in matlab function arguments as far as I know, unless they're being used within [] to construct arrays.
maskvector=treenumber==tree;
treeindices=find(maskvector);
lengthtreepositions=sum(maskvector);
remainingpositionxs=inputpositionxs(find(maskvector==0));
remainingpositionys=inputpositionys(find(maskvector==0));
I'll start by explaining what this does - I think you've got it, but just to be sure.
It all works by joining together trees. Each tree grows and is joined to nearby ones. This involves looking around each tree for nodes from other trees that are nearby. The first step here is to isolate the nodes of one tree. treeindices does this. lengthtreepositions is the number of nodes in this one tree, and the for loop a few lines later is where the searching is done, it visits each node of the tree, so i counts to lengthtreepositions.
Taking a look at this code here (and let's assume 5 points/trees to start with and that the tree we're dealing with is number 2). Does this mean that:
maskvector equals [0,0,1,0,0].
treeindices equals 3 (the only non-zero in the vector
That's right, assuming you meant tree number 3.
lengthtreepositions equals... wait a minute? Does maskvector become something like:
0,0,1,0,0
0,0,1, 0
0, 1, 0
*kicks self* I should have explained - the sum function arithmetically adds together all the elements of a vector (I don't think I use it on a matrix anywhere here).
maskvector remains a vector, and
lengthtreepositions is a scalar equal to the number of nodes contained in the current tree.
So first time around lenghtreepositions will equal 1 and later on more elements are added to the columns?
lenghtreepositions starts at one and increases as the number of ones in
maskvector grows.
and finally remainingpositionxs and remainingpositionys both become [1,2,4,5]?
They'll become the xs and ys corresponding to the indices [1,2,4,5], yes.
Lasty, a general matlab question: Can an element in a matrix be a matrix itself? I haven't worked out whether branches is using something like that or not.
No, but I can totally see what you're thinking. Matrices can be put together in the same way as scalars to form larger matrices. The individual elements of the ``ingredient" matrices then become individual elements of the resultant matrix. If I make a 4-by-4 matrix
a, and another 4-by-4 matrix
b,
c=[a,b] would be a 4-by-8 matrix.
I'll go through exactly how branches evolves. It does indeed do the sort of thing I just described.
branches=[];
At the beginning, initialised as a 0-by-0 array.
mindistancecandidates=[mindistancecandidates;mindistancecandidate];
mindistancecandidateindices=[mindistancecandidateindices;branchcandidateindex];
When searching around the nodes of a particular tree to find potential linkees, many distances are stored so they can be compared later (we want the minimum distance). These distances are
mindistancecandidates, and their indices are
mindistancecandidateindices. We get this pair for each node of the current tree.
mindistance=[min(mindistancecandidates),treeindex,mindistancecandidateindices(find(mindistancecandidates==min(mindistancecandidates)))]; % minimum distances carry the indices of the prospective link destination
mindistances=[mindistances;mindistance];
Once we have all the neighbouring distances, we find the minimum, which is
mindistance. It's a row vector, at this point I get it to carry the 2 indices of the potential link nodes,
treeindex and . . . the other one.
mindistance is the minimum distance for a single node of the tree.
mindistances stores the minimum distances for all nodes of the current tree.
branchdistance=min(mindistances(:,1));
branchindex=find(branchdistance==mindistances(:,1));
These are the precursors to
branch. For the current tree, we want the minimum distance from any node of that tree to any node outside that tree. This means we want the minimum (
branchdistance) of the minima (
mindistances) found at each node.
branch=mindistances(branchindex,:);
Here, the appropriate row of
mindistances is selected and then -
branches=[branches;branch];
This is where
branch is added as a row to
branches. The final result of the code is a matrix with as many rows as there are MST links, and 3 columns. Each row has the length of the link and the indices of the two endpoints.
I'm sort of rubber ducking here/thinking aloud/whatever but I'm slowly starting to understand. Please be patient, there's going to be a lot more questions yet...
No worries

, I don't visit this place every day though, so I won't always be able to reply straight away.
In other news, this thread has broken into the forum's top 10!