Matlab plot is drawing an individual line to each data point
46 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alexandr
am 24 Jun. 2024
Bearbeitet: Aquatris
am 24 Jun. 2024
So im writing a matlab script that creates a 3 by 100 matrix x and a 1 by 100 matrix n. When I try to plot it, matlab draws an individual line segment from the origin to each data point, rather than just connecting the data points. From what I found online, it's due to the plot function being inside a for loop, but after I moved it outside the loop it kept doing the same thing. Can somebody help.
Here's what the plot looks like
0 Kommentare
Akzeptierte Antwort
Aquatris
am 24 Jun. 2024
Bearbeitet: Aquatris
am 24 Jun. 2024
You have a lot of 0s in your nc and x vector, hence you go back and forth to 0 position. You can see this if you use a data point in one of the lines and move along the data using keyboard arrow keys.
The reason is because your if statement is not always true, so you do not fill your nc and x vector in every run, so it fills it as default 0 for nc and you already prefilled x with 0s. You can either use an idx variable to find non zero entries as the code below, or change the way you store the values. I have no idea what you are trying to do so I am not sure if this is actually what you want.
%% EXAMPLE 1
n = 2; % n can only be a prime number
x = zeros(3,100); % initialize array with rows as cases for p
p = [80/100,85/100,88/100];
nc = x(1,:);
while (n < 100)
if isprime(n)
for i = 1:3
for j = n:-1:1
x(i,n) = (x(i,n) + j)^p(i);
nc(n) = n;
end
end
n = n+1;
else
n = n+1;
end
end
idx = nc~=0; % find non zero nc entries
plot(nc(idx),x(:,idx),'--o')
grid on
legend('Location','best')
xlabel('n')
ylabel('x_n')
title({'Lab1_A'})
0 Kommentare
Weitere Antworten (1)
Animesh
am 24 Jun. 2024
Hi Alexandr,
It sounds like you might be using the plot function incorrectly in MATLAB. To plot a continuous line connecting your data points, you need to provide the x and y coordinates of your data points as vectors. Here’s a basic example of how to do it correctly:
% Sample data points
x = [1, 2, 3, 4, 5];
y = [2, 4, 6, 8, 10];
% Plotting the data points with a continuous line
figure;
plot(x, y, '-o'); % '-o' creates a line with circle markers at data points
xlabel('X-axis label');
ylabel('Y-axis label');
title('Plot of Data Points');
grid on;
In this example:
- x and y are vectors containing the coordinates of the data points.
- The '-o' argument specifies that MATLAB should draw a line connecting the data points and place a circle marker at each data point.
If you want to plot multiple sets of data on the same graph, you can do so by including additional pairs of x and y vectors in the plot function call:
% Additional data points
x2 = [1, 2, 3, 4, 5];
y2 = [3, 6, 9, 12, 15];
% Plotting multiple sets of data points
figure;
plot(x, y, '-o', x2, y2, '-x'); % '-x' creates a line with x markers at data points
xlabel('X-axis label');
ylabel('Y-axis label');
title('Plot of Multiple Data Sets');
legend('Data Set 1', 'Data Set 2');
grid on;
Make sure that your x and y vectors are of the same length, as MATLAB requires this to plot the points correctly.
For more information on plot function, please consider going through the documentation: https://in.mathworks.com/help/matlab/ref/plot.html
1 Kommentar
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!