Apply pattern to Matrix Indexes in For Loop
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Olivia Licata
el 20 de Dic. de 2017
Comentada: Star Strider
el 23 de Dic. de 2017
Hello, I have a list of values in groups of three. Imagine a column [x1 x2 x3... x112146] I would like to plot x versus y of
(x1,x2) (x2, x1), (x1, x3), (x3,x1), (x2, x3), (x3, x2)
The next group of three would be
(x4, x5), (x5,x4), (x6, x4), (x4, x6), (x5, x6), (x6,x5)
The order within groupings doesn't really matter. I was able to create a pattern for my previous plot with groupings of two by establishing indices and then sorting by even or odd. (code below)
xy = zeros(length(za_twos),2);
xy(:,1)=za_twos(:,1);
for i = 2:(length(za_twos)+1)
if mod(i,2)==0
xy(i-1,2)=za_twos(i,1);
else
xy(i-1,2)=za_twos(i-2,1);
end
end
This returned
(x1, x2) (x2, x1) (x3, x4) (x4, x3)
(in two columns representing x and y)
0 comentarios
Respuesta aceptada
Star Strider
el 20 de Dic. de 2017
See if this does what you want:
V = (1:18)'; % Original Vector
Vr = reshape(V, 3, []); % Reshape To (3xN) Matrix
idx = nchoosek(1:3,2); % Create Index Permutations - Half
idx = [idx; fliplr(idx)]; % Create Index Permutations - All
hax = axes('NextPlot', 'add');
for k1 = 1:size(Vr,2)
plot(hax, Vr(idx(:,1), k1), Vr(idx(:,2), k1), 'p')
end
I used a linear vector to test the code, so after you run this to be certain if it produces the result you want, substitute your own vector for ‘V’. (The length of your vector is evenly divisible by 3, so my code here should work with your column vector.) I plotted them all on the same axes.
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!