Clear Filters
Clear Filters

Traversing Through a Vector of Different-sized Cells

1 view (last 30 days)
Hi,
Let me first explain the code I want to execute. I have a matrix "V" of size (m x 2), where the first column is x values and the second column is y values. I also have a complex vector of different sized cells called "C". This vector contains indices of V, where each row can be of a different length. So, as an example:
V =
20 40
10 20
5 10
30 15
25 35
C =
1 2 3
5 2 3 1
3 4 2 1
1 5
I would like to draw a line between the coordinates in "V" corresponding to the indices in all of the rows of "C".
Therefore, the last row of "C" should look similar to this:
line([V(1,1) V(1,2)], [V(5,1) V(5,2)])
Is there a way to write some general code that fits this method?
If it helps, I am using this principle to draw Voronoi edges. "V" means the location of the vertices (coordinates) and each row in "C" corresponds to every vertex contained in each cell.
Thanks, Ian

Accepted Answer

Hugo
Hugo on 1 Jul 2013
Assuming that
V =[20 40 10 20 5 10 30 15 25 35];
C ={ {1 2 3}; {5 2 3 1}; {3 4 2 1}; {1 5}};
You can use:
line(V([C{i}{:}],1),V([C{i}{:}],2));
to draw any line based on the indexes in each row in C.
  5 Comments
Hugo
Hugo on 2 Jul 2013
The index i corresponds to C, so the for loop should go from 1 to the number of rows in C. That way, there should be any error. Notice that in the example you wrote, C has four rows, and thus, if you set the loop to go from 1 to the number of rows in V, then the index i will reach 5, which is greater than the number of rows in C.
The first line of V being infinity does not produce an error in line. So what to do with that line is something you need to decide based on what you want to plot. There's no technical problem with that. Hope this helps.
Ian Wood
Ian Wood on 2 Jul 2013
Edited: Ian Wood on 2 Jul 2013
Ah right, I'm sorry. That was a real rookie mistake. Thanks for your help Hugo!
Just so it's clear, the full correct code is the following:
for i=1:length(C)
line(V([C{i,:}],1),V([C{i,:}],2));
end

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!