How do I plot six different matrices?

2 次查看(过去 30 天)
I have 6 different matrixes, 22x1 each one. When I plot them I get this:
plot(x1,y1,'o',x2, y2,'o', x3, y3,'o') %%Using this code
What I need to have is this:
In other words, I need to connect, for example, [x1(1) y1(1)] [x2(1) y2(1)],[x3(1) y3(1)].
I used this but it didn't work
for i=1:1 plot([x1(i) y1(i)], [x2(i) y2(i)] ,[x3(i) y3(i)])
end

采纳的回答

Image Analyst
Image Analyst 2019-1-6
It looks like your data is already sorted, so we can skip the sorting/ordering in your specific case, at least for this data sample, and just draw lines:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
x1=[
1
1
1
1
0.999999912
0.999690873
0.998202938
0.996110802
0.992961257
0.980004982
0.924939672
0.820535897
0.602167816
0.398690056
0.203859669
-0.075955146
-0.304796417
-0.557870458
-0.864153125
-0.99993919
-0.988967944
-0.999529311]
y1=[
0
-2.54E-10
-3.15E-07
-1.62E-05
-0.000419996
-0.02486278
-0.059924071
-0.088109422
-0.118439611
-0.198972951
-0.380113933
-0.571594998
-0.798369539
-0.917085732
-0.97900012
-0.997111235
-0.952417526
-0.82992804
-0.50322895
-0.011027972
0.14812969
0.030678273]
x2=[
2
2
2
2
1.999999912
1.999690873
1.998202929
1.996110712
1.992960745
1.979993679
1.924375819
1.81311973
1.510835006
0.912920593
0.344944452
-0.139948732
-0.785972062
-1.356710455
-1.804704529
-1.980245442
-1.987180774
-1.998004715]
y2=[
0
-2.54E-10
-3.15E-07
-1.62E-05
-0.000419996
-0.024874491
-0.060061709
-0.08853234
-0.119452311
-0.203727586
-0.413690537
-0.693157058
-1.215890722
-1.774737732
-1.968997637
-1.995061545
-1.829041733
-1.431471605
-0.842880325
-0.208511267
0.088370616
0.085876712]
x3=[
3
3
3
3
2.999999912
2.999690873
2.998202929
2.996110712
2.992960745
2.979993678
2.924375609
2.813103613
2.509294444
1.843529705
0.621048704
-0.782583788
-1.680485396
-2.277495171
-2.690548186
-2.858916151
-2.893450016
-2.924109296]
y3=[
0
-2.54E-10
-3.15E-07
-1.62E-05
-0.000419996
-0.024874492
-0.060061779
-0.088532791
-0.119454264
-0.203752975
-0.414339097
-0.698834419
-1.27137721
-2.140752324
-2.93012533
-2.76123397
-2.276082999
-1.821542761
-1.306864177
-0.685939571
-0.334330296
-0.291390175]
plot(x1, y1, 'ro', 'LineWidth', 2);
hold on;
plot(x2, y2, 'ko', 'LineWidth', 2);
plot(x3, y3, 'bo', 'LineWidth', 2);
% Fit a circle to the innermost
[xc,yc,R,a] = circfit(x1, y1);
plot(xc, yc, 'r+', 'MarkerSize', 30, 'LineWidth', 2);
grid on;
% Draw lines from x1, y1 to xc, yc
for k = 1 : length(x1)
hold on;
plot([x1(k), xc], [y1(k), yc], 'r-');
end
% Draw lines from x2, y2 to x1, y1. Same for x3, y3.
for k = 1 : length(x1)
plot([x2(k), x1(k)], [y2(k), y1(k)], 'r-', 'LineWidth', 2);
plot([x2(k), x3(k)], [y2(k), y3(k)], 'b-', 'LineWidth', 2);
end
function [xc,yc,R,a] = circfit(x,y)
%CIRCFIT Fits a circle in x,y plane
%
% [XC, YC, R, A] = CIRCFIT(X,Y)
% Result is center point (yc,xc) and radius R. A is an optional
% output describing the circle's equation:
%
% x^2+y^2+a(1)*x+a(2)*y+a(3)=0
% by Bucher izhak 25/oct/1991
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];
B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];
a=A\B;
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
end
0001 Screenshot.png
  1 个评论
Arda Nova
Arda Nova 2019-1-6
Yes, it was calculated beforehand. Half of the code you wrote, I don't even know, but I will learn for sure since I will need it for other projects of mine. I sincerely thank you, for I didn't know that it was this much troublesome to do. I thought it would be something like do the plot this way, and you are good to go. Again, thank you very much.

请先登录,再进行评论。

更多回答(3 个)

Star Strider
Star Strider 2019-1-6
This should get you started:
t = linspace(0, pi, 20); % Create Data
x = 3*cos(t); % Create Data
y = -3*sin(t); % Create Data
figure
plot(x, y, 'o', 'MarkerFaceColor','b')
hold on
plot([ones(size(x))-1; x], [ones(size(y))-0.5; y], '-b')
plot(0, 0.5, 'ob', 'MarkerFaceColor','b')
hold off
axis equal
The Plot —
How do I plot this - 2019 01 06.png
The idea is to plot individual lines from whatever you define as the origin (here: (0,0.5)), and the individual circles. The second plot call does that, using matrix arguments that correspond to [x_origin; x_destination], and [y_origin; y_destination].
  2 个评论
Star Strider
Star Strider 2019-1-6
The origin appears to be at (0,0). If it is somewhere else, you can easily adapt my code to change it. (I changed your ‘x’ vector to ‘x1’ to make it compatible with the rest of the code. I also changed the comma decimal separators to period.)
Now, with your data, this code:
figure
plot([zeros(size(x1)), x1]', [zeros(size(y1)), y1]','-ok', [x1, x2]', [y1, y2]','-ok', [x2, x3]', [y2, y3]','-ok', 'MarkerFaceColor','k')
axis equal
xlim([-3.5, 3.5])
produces this plot:
How do I plot this (2) - 2019 01 06.png
Chjange the circle colors if you want to. I chose to make them black to match the plot image you posted.

请先登录,再进行评论。


Image Analyst
Image Analyst 2019-1-6
What I would to is
  1. Take the inner most x and y vectors (let's call them x1 and y1) and fit to a circle. Use The FAQ. Now you should have the radii and the center.
  2. Then I would convert each of the 3 arrays to polar coordinates where the center/origin is the center of the innermost set.
  3. Then I would sort the points in each of the 3 sets by angle and convert back to cartesian coordinates. Now you have the points ordered from top left to bottom middle to top right.
  4. Then I'd have a loop where I go along the sorted x1, y1 arrays using plot() or line() to draw a line to the center point you found in setp 1.
  5. Then I'd have a loop over the second set of sorted x2,y2 (the middle set) where you draw a line, with plot() or line() to the corresponding index in the innermost (x1,y1) set, and the outermost (x3, y3) set. Now all lines are drawn.
Then you should be done. Note: this assumes that all vectors are the same length. You seem assured of this, but just to be robust, you should do a check first and throw an error to warn the user if they're not the same length.
I didn't use distance to closest point since some distances were quite similar and you looked like you wanted it to be ordered and not have any points skipped, so that's why I sorted and went down the sorted lists in a for loop.
Pretty easy algorithm, I think, but if you still have trouble, attach your x and y data.
  1 个评论
Arda Nova
Arda Nova 2019-1-6
It's a triple pendulum actually. Numbers taken from another code. Sorry, I couldn't really understand the steps, I am pretty new to matlab. I have given the numbers under "GMD Baloch " 's answer. I thought it would be easy. I was going to use paint :D, but wanted to try my chance here.

请先登录,再进行评论。


GMD Baloch
GMD Baloch 2019-1-6
Can you provide me the data you want to plot, so that I may understand the problem well and help you
  5 个评论
Arda Nova
Arda Nova 2019-1-7
It works, and it's simple too. I really liked it. Thank you very much :)
GMD Baloch
GMD Baloch 2019-1-7
You are welcome and if you liked my work kindly accept my answer

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by