Traversing Through a Vector of Different-sized Cells

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

 Respuesta aceptada

Hugo
Hugo el 1 de Jul. de 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 comentarios

Ian Wood
Ian Wood el 1 de Jul. de 2013
Editada: Ian Wood el 1 de Jul. de 2013
This looks more like it, unfortunately the program is experiencing an error when I try this method. It says "cell contents reference from a non-cell array object", and I am not sure why because "C" is a cell array that I am referencing. Maybe it's because "V" is not a cell array? Maybe I have to concatenate my x and y values into a single array like you did in your example. Any ideas on how to fix these issues? Thanks for your reply.
Hugo
Hugo el 1 de Jul. de 2013
First of all, I apologise I wrote V wrong. The example that I wrote should have been:
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}}; line(V([C{i}{:}],1),V([C{i}{:}],2));
where i should be the index of one row in C. Notice that now V is a m x 2 matrix as you mentioned in your question.
The error may be due to how you construct C. In my example, C is a cell of cells. In my example, the first row is C{1}{:}. How is it in your code? Perhaps it is C{1,:}. In that case, you need to change that in the code accordingly, replacing C{i}{:} with C{i,:}.
Ian Wood
Ian Wood el 1 de Jul. de 2013
Editada: Ian Wood el 1 de Jul. de 2013
I was afraid of this, I fixed the problem and in my code yes it is written like C{1,:} but it comes up with one error. The error is the "index exceeds matrix dimensions" when written like so:
line(V([C{i,:}],1),V([C{i,:}],2));
I wrote this in a for loop going from index 1 to the length of "V". However, the first row index in "V" in my code is infinity(x) by infinity(y). I'm thinking that also is creating a problem since MATLAB literally cannot find infinity. I don't know how else to draw these edges, unless I give an if statement to the first index of "V" to be ignored. Thoughts?
Hugo
Hugo el 2 de Jul. de 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 el 2 de Jul. de 2013
Editada: Ian Wood el 2 de Jul. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Preguntada:

el 1 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by