代码之家  ›  专栏  ›  技术社区  ›  user3743825

绘制单元格数组

  •  1
  • user3743825  · 技术社区  · 8 年前

    我需要在Matlab中使用以下格式绘制单元格数组:

    {[vector1], [vector2], ...}
    

    以向量索引为y,向量为x的2D图

    ([vector1], 1), ([vector2], 2), ...
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   EBH    8 年前

    这里有一个简单的选项:

    % 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 值:

    A cell plot

    编辑:对于不相等矢量

    您必须使用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
    

    Cell plot 2

    希望这能回答您的问题;)

        2
  •  1
  •   Edward Carney    8 年前

    这是我对你的请求的(暴力)解释。可能有更优雅的解决方案。

    这段代码生成一个点图,将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
    

    enter image description here

        3
  •  0
  •   user3716193    8 年前

    在没有任何数据的情况下,这是我能为您提供的最佳数据:

    yourCell = {[0,0,0],[1,1,1],[2,2,2]}; % 1x3 cell
    figure; 
    plot(cell2mat(yourCell));
    ylabel('Vector Values'); 
    xlabel('Index of Vector');
    

    它的情节如下:

    enter image description here

    希望这能有所帮助。