问题
Consider a 1x3 cell A:
A = { [A1] [A2] [A3] }
A = {[1 2 3; 4 5 6; 7 8 9] [6 5 4; 9 8 7] [1 1 1]}
where the structure of Ai is such that:
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
How to plot all these points such that we use one color for all the points of A1, another color for all the points of A2 and some other color for all the points of A3?
In general, if we have a 1xn cell i.e. A = { [A1] [A2] [A3] ... [An] }, how can this be done?
回答1:
Concatenate all matrices inside the cell array 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 to plot these points.
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');
For the given A, the above code produces this result:
来源:https://stackoverflow.com/questions/46065598/how-to-plot-each-matrix-in-a-cell-with-a-different-color-in-a-3d-plot