简体   繁体   English

在Matlab中绘制点行

[英]Plotting rows of points in Matlab

So I'm still getting used to Matlab and am having a bit of trouble with plotting. 因此,我仍然习惯于Matlab,并且在绘制时遇到了一些麻烦。 I have a cell which contains a list of points in each row. 我有一个单元格,其中每行包含一个点列表。 I want to plot each row of points in a different colour on the same graph so I can compare them. 我想在同一张图表上以不同的颜色绘制点的每一行,以便可以对其进行比较。 The catch is that I need to make this work for an unknown number of points and rows (ie the number of points and rows can change each time I run the program). 问题是,我需要针对未知数量的点和行进行此工作(即,每次运行程序时,点和行的数量可以更改)。

So for example, I might have my cell array A: 因此,例如,我可能拥有单元格数组A:

A = {[0,0], [1,2], [3,4]; A = {[0,0],[1,2],[3,4]; [0,0] [5,6], [9,2]} [0,0] [5,6],[9,2]}

and I want to plot the points in row 1 against their index (so a 3D graph) and then have the points in row 2 on the same graph in a different colour. 我想针对它们的索引绘制第1行中的点(因此是3D图形),然后在同一图形上使第2行中的点具有不同的颜色。 The rows will always be the same length. 行将始终具有相同的长度。 (Each row will always have the same number of points). (每行将始终具有相同数量的点)。 I've tried a few different for loops but just can't seem to get this right. 我已经尝试了一些不同的for循环,但似乎无法正确解决这个问题。

Any help in sending me in the right direction would be greatly appreciated! 向我发送正确方向的任何帮助将不胜感激!

You could just convert it to a matrix and plot it directly: 您可以将其转换为矩阵并直接绘制:

% Some dummy data - format a little different from your example 
% to allow for different numbers of elements per row
A = {[0,0, 1,2, 3,4]; [0,0, 5,6]};

% Figure out how many columns we need in total
maxLen = max(cellfun(@length, A));
% Preallocate
Amat = NaN(size(A, 1), maxLen);
% Copy data
for n = 1:size(A, 1)
    curA = A{n}; 
    Amat(n, 1:length(curA)) = curA; 
end

% Generate 1:N vector repeated the correct number of times (rows)
x = repmat(1:size(Amat, 2), size(Amat, 1), 1);
plot(x, Amat)

Edit: You mentioned a 3D graph at some point in your post. 编辑:您在帖子的某个位置提到了3D图形。 The above won't plot a 3D graph, so here's something that will: 上面的内容不会绘制3D图形,因此可以:

% Generate Amat as above

% Then:
[X, Y] = meshgrid(1:size(Amat, 1), 1:size(Amat, 2));
surf(X, Y, Amat.'); % OR: plot3(X, Y, Amat.');

I'm not sure this is exactly what you want, but your question is slightly unclear on exactly what kind of graph you want out of this. 我不确定这正是您想要的,但是对于您想要从中得到哪种图形的问题,您的问题仍然不清楚。 If you just want coloured lines on your plot, you can use plot3 instead of surf , but IMHO surf will probably give you a clearer plot for this kind of data. 如果只想在绘图上使用彩色线条,则可以使用plot3而不是surf ,但是IMHO surf可能会为您提供此类数据的更清晰绘图。

The fact that the number of points and rows can change with each iteration should not pose much of a problem. 点和行的数量随每次迭代而改变的事实应该不会造成太大的问题。 I would suggest using the size function before your plot loops (size(A,1) and size(A,2)) to get the dimensions of the matrix. 我建议在绘图循环之前使用size函数(size(A,1)和size(A,2))以获取矩阵的尺寸。

Once you have the size of the matrix, loop through the dimensions and plot the lines on the same plot using holdon, and then finally just make the line color a function of the dimensions as you loop through so that you always have a different line color 有了矩阵的大小后,遍历尺寸并使用holdon在同一图上绘制线,最后在遍历时仅使线颜色成为尺寸的函数,以便始终具有不同的线色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM