Matlab 如何在3D绘图中使用不同颜色绘制单元格中的每个矩阵?
考虑一个1x3单元Matlab 如何在3D绘图中使用不同颜色绘制单元格中的每个矩阵?,matlab,plot,colors,matlab-figure,Matlab,Plot,Colors,Matlab Figure,考虑一个1x3单元a: A = { [A1] [A2] [A3] } A = {[1 2 3; 4 5 6; 7 8 9] [6 5 4; 9 8 7] [1 1 1]} 其中,Ai的结构如下: A1 = [ 1 2 3 %coordinate (x,y,z) of point 1 4 5 6 %coordinate (x,y,z) of point 2 7 8 9 ] %coordinate (x,y,z) of point 3 A2
a
:
A = { [A1] [A2] [A3] }
A = {[1 2 3; 4 5 6; 7 8 9] [6 5 4; 9 8 7] [1 1 1]}
其中,Ai
的结构如下:
A1 = [ 1 2 3 %coordinate (x,y,z) of point 1
4 5 6 %coordinate (x,y,z) of point 2
7 8 9 ] %coordinate (x,y,z) of point 3
A2 = [ 6 5 4 %coordinate (x,y,z) of point 4
9 8 7 ] %coordinate (x,y,z) of point 5
A3 = [ 1 1 1 ] %coordinate (x,y,z) of point 6
如何绘制所有这些点,以便我们对A1
的所有点使用一种颜色,对A2
的所有点使用另一种颜色,对A3
的所有点使用其他颜色
一般来说,如果我们有一个1xn单元,即
a={[A1][A2][A3]…[An]}
,如何做到这一点?将单元数组a
中的所有矩阵串联起来。使用或为不同的矩阵生成不同的颜色。在A
中找到每个矩阵中的点数,以确定每个颜色的重复次数。相应地生成每种颜色的份数,并最终用于绘制这些点
newA = vertcat(A{:}); %Concatenating all matrices inside A vertically
colours = jet(numel(A)); %Generating colours to be used
colourtimes = cellfun(@(x) size(x,1),A); %Determining num of times each colour wil be used
colourind = zeros(size(newA,1),1); %Zero matrix with length equals num of points
colourind([1 cumsum(colourtimes(1:end-1))+1]) = 1;
colourind = cumsum(colourind); %Linear indices of colours for newA
scatter3(newA(:,1), newA(:,2), newA(:,3),[], colours(colourind,:),'filled');
对于给定的A
,上述代码生成以下结果: