按列对矩阵行进行分组,并绘制行描述的路径

时间:2017-02-02 19:15:02

标签: matlab matrix plot 3d matlab-figure

我的矩阵paths有四列:第一列有一个数字表示该行所属的path,其他三个是维度。我想打印paths。我这样做:

for p = min(paths(1,:)):max(paths(1,:))
    path = paths(paths(:,1)==p,:);
    plot3(path(:,2),path(:,3),path(:,4),'Color','k');
end

考虑到一些函数式编程,我想知道:如何将矩阵paths分组到第一列并打印每个组的路径?

1 个答案:

答案 0 :(得分:0)

你非常接近它。首先,您需要遍历第一个中的值(而不是问题中的),并为仅采用相关路径创建逻辑索引,绘制它们(使用hold):

paths = [randi(4,50,1) rand(50,3)]; % example data
for p = unique(paths(:,1)).'
  path = paths(:,1)==p; % a logical index for path p
  plot3(paths(path,2),paths(path,3),paths(path,4));
  hold on % to plot all the paths on the same axes
end
hold off % stop plotting on this axes

以下是输出的示例:

enter image description here