Apply pattern to Matrix Indexes in For Loop
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2017-12-20
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!