绘制元胞数组
Plotting a cell array
我需要在 Matlab 中绘制具有以下格式的元胞数组:
{[vector1], [vector2], ...}
以向量索引为 y 向量为 x 的二维图形
([vector1], 1), ([vector2], 2), ...
在没有任何数据的情况下,这是我能为你想要的最好的:
yourCell = {[0,0,0],[1,1,1],[2,2,2]}; % 1x3 cell
figure;
plot(cell2mat(yourCell));
ylabel('Vector Values');
xlabel('Index of Vector');
它的情节是这样的:
希望对您有所帮助。
这是我对您的请求的(强力)解释。可能有更优雅的解决方案。
此代码生成一个点图,将向量中每个索引处的值放在 y 轴上(从下到上)。它可以容纳不同长度的向量。您可以将其设为向量分布的点图,但如果可能出现多次相同或几乎相同的值,则可能需要向 x 值添加一些抖动。
% random data--three vectors from range 1:10 of different lengths
for i = 1:3
dataVals{i} = randi(10,randi(10,1),1);
end
dotSize = 14;
% plot the first vector with dots and increase the dot size
% I happen to like filled circles for this, and this is how I do it.
h = plot(dataVals{1}, ones(length(dataVals{1}), 1),'.r');
set(h,'markers', dotSize);
ax = gca;
axis([0 11 0 4]); % set axis limits
% set the Y axis labels to whole numbers
ax.YTickLabel = {'','','1','','2','','3','','',}';
hold on;
% plot the rest of the vectors
for i=2:length(dataVals)
h = plot(dataVals{i}, ones(length(dataVals{i}),1)*i,'.r');
set(h, 'markers', dotSize);
end
hold off
这是一个简单的选项:
% some arbitrary data:
CellData = {rand(10,1)*50,rand(10,1)*50,rand(10,1)*50};
% Define x and y:
x = cell2mat(CellData);
y = ones(size(x,1),1)*(1:size(x,2));
% plot:
plot(x,y,'o')
ylim([0 size(x,2)+1])
因此您将 x
的每个向量绘制在单独的 y
值上:
只要您的元胞数组只是一个向量列表,它就可以工作。
编辑:对于不相等的向量
你必须使用 for 循环 hold
:
% some arbitrary data:
CellData = {rand(5,1)*50,rand(6,1)*50,rand(7,1)*50,rand(8,1)*50,rand(9,1)*50};
figure;
hold on
for ii = 1:length(CellData)
x = CellData{ii};
y = ones(size(x,1),1)*ii;
plot(x,y,'o')
end
ylim([0 ii+1])
hold off
希望这能回答您的问题 ;)
我需要在 Matlab 中绘制具有以下格式的元胞数组:
{[vector1], [vector2], ...}
以向量索引为 y 向量为 x 的二维图形
([vector1], 1), ([vector2], 2), ...
在没有任何数据的情况下,这是我能为你想要的最好的:
yourCell = {[0,0,0],[1,1,1],[2,2,2]}; % 1x3 cell
figure;
plot(cell2mat(yourCell));
ylabel('Vector Values');
xlabel('Index of Vector');
它的情节是这样的:
希望对您有所帮助。
这是我对您的请求的(强力)解释。可能有更优雅的解决方案。
此代码生成一个点图,将向量中每个索引处的值放在 y 轴上(从下到上)。它可以容纳不同长度的向量。您可以将其设为向量分布的点图,但如果可能出现多次相同或几乎相同的值,则可能需要向 x 值添加一些抖动。
% random data--three vectors from range 1:10 of different lengths
for i = 1:3
dataVals{i} = randi(10,randi(10,1),1);
end
dotSize = 14;
% plot the first vector with dots and increase the dot size
% I happen to like filled circles for this, and this is how I do it.
h = plot(dataVals{1}, ones(length(dataVals{1}), 1),'.r');
set(h,'markers', dotSize);
ax = gca;
axis([0 11 0 4]); % set axis limits
% set the Y axis labels to whole numbers
ax.YTickLabel = {'','','1','','2','','3','','',}';
hold on;
% plot the rest of the vectors
for i=2:length(dataVals)
h = plot(dataVals{i}, ones(length(dataVals{i}),1)*i,'.r');
set(h, 'markers', dotSize);
end
hold off
这是一个简单的选项:
% some arbitrary data:
CellData = {rand(10,1)*50,rand(10,1)*50,rand(10,1)*50};
% Define x and y:
x = cell2mat(CellData);
y = ones(size(x,1),1)*(1:size(x,2));
% plot:
plot(x,y,'o')
ylim([0 size(x,2)+1])
因此您将 x
的每个向量绘制在单独的 y
值上:
只要您的元胞数组只是一个向量列表,它就可以工作。
编辑:对于不相等的向量
你必须使用 for 循环 hold
:
% some arbitrary data:
CellData = {rand(5,1)*50,rand(6,1)*50,rand(7,1)*50,rand(8,1)*50,rand(9,1)*50};
figure;
hold on
for ii = 1:length(CellData)
x = CellData{ii};
y = ones(size(x,1),1)*ii;
plot(x,y,'o')
end
ylim([0 ii+1])
hold off
希望这能回答您的问题 ;)