如何在 3D 图中用不同颜色绘制单元格中的每个矩阵?
How to plot each matrix in a cell with a different color in a 3D plot?
考虑一个 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 = [ 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
的所有点使用另一种颜色,A2
的所有点使用另一种颜色 A3
?
一般来说,如果我们有一个 1xn 的单元格,即 A = { [A1] [A2] [A3] ... [An] }
,该怎么做?
连接元胞数组 A
vertically. Use jet
or any other colormap to generate different colours for different matrices. Find the number of points in each matrix inside A
to determine number of times each colour will be repeated. Generate number of copies of each colour accordingly and finally use scatter3
内的所有矩阵以绘制这些点。
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
,上面的代码产生了这个结果:
考虑一个 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 = [ 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
的所有点使用另一种颜色,A2
的所有点使用另一种颜色 A3
?
一般来说,如果我们有一个 1xn 的单元格,即 A = { [A1] [A2] [A3] ... [An] }
,该怎么做?
连接元胞数组 A
vertically. Use jet
or any other colormap to generate different colours for different matrices. Find the number of points in each matrix inside A
to determine number of times each colour will be repeated. Generate number of copies of each colour accordingly and finally use scatter3
内的所有矩阵以绘制这些点。
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
,上面的代码产生了这个结果: