40 次查看(过去 30 天)
显示 更早的评论
Alexandr 2024-6-24,1:46
编辑: Aquatris 2024-6-24,9:07
采纳的回答: Aquatris
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 个评论 显示 -2更早的评论隐藏 -2更早的评论
显示 -2更早的评论隐藏 -2更早的评论
请先登录,再进行评论。
请先登录,再回答此问题。
采纳的回答
Aquatris 2024-6-24,8:34
⋮
编辑:Aquatris 2024-6-24,9:07
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 个评论 显示 -2更早的评论隐藏 -2更早的评论
显示 -2更早的评论隐藏 -2更早的评论
请先登录,再进行评论。
更多回答(1 个)
Animesh 2024-6-24,2:39
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 个评论 显示 -1更早的评论隐藏 -1更早的评论
显示 -1更早的评论隐藏 -1更早的评论
Alexandr 2024-6-24,3:21
此评论的直接链接
https://ww2.mathworks.cn/matlabcentral/answers/2131276-matlab-plot-is-drawing-an-individual-line-to-each-data-point#comment_3193966
Thank you for your answer Animesh, however my code is correct based on what you replied here. I am attaching my code with this reply, so you could take a closer look. Thanks again!
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
plot(nc,x,'--o')
grid on
legend('Location','best')
xlabel('n')
ylabel('x_n')
title({'Lab1_A'})
请先登录,再进行评论。
请先登录,再回答此问题。
发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 简体中文Chinese
- English
- 日本Japanese (日本語)
- 한국Korean (한국어)
联系您当地的办事处