Apply pattern to Matrix Indexes in For Loop

3 visualizzazioni (ultimi 30 giorni)
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)

Risposta accettata

Star Strider
Star Strider il 20 Dic 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 Commenti
Olivia Licata
Olivia Licata il 23 Dic 2017
Thank you! This did work, it took a little long since the plotting went stepwise through the loop, but the index permutations were very helpful and I was able to adjust this for groupings 4-10 as well. I changed the plot to a scatter plot and adjusted all the points to one color for personal preferences. Thank you so much!
Star Strider
Star Strider il 23 Dic 2017
As always, my pleasure!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by