2017-09-06 28 views
1

考慮一個1×3單元A如何在3D圖中用不同顏色的單元格繪製每個矩陣?

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] },這個怎麼做?

回答

1

連接單元陣列內的所有矩陣Avertically。使用jetany other colormap爲不同的矩陣生成不同的顏色。查找A中每個矩陣中的點數,以確定每種顏色重複的次數。相應地生成每種顏色的副本數量,最後使用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,上面的代碼產生這樣的結果:

output

相關問題