2016-03-22 17 views
2

kullanarak tanımlayın Bir dairem var ve bu daireyi 3 sektöre ayırıyorum (her biri 120 açı). Daire alanında, bilinen bazı noktalar var (Nokta koordinatları biliniyor). hangi noktanın hangi sektöre ait olduğunu bilir. Bir çıktı olarak bireysel sektörün noktasını elde etmek istiyorum.Farklı daire dairelerinin noktalarını MATLAB

Daireyi çiziyorum, sektöre bölün ve bazı noktaları çiziyorum. Ama hangi noktanın hangi sektöre ait olduğunu belirleyemedim. Bunu nasıl alabilirim? Bu işlem için MATLAB kullanarak herhangi bir kaynak kodunuz varsa, bana çok yardımcı olur. İşte

, benim kaynak kodu almalı

x0 = 2;  % origin x-coordinate 
y0 = 1;  % origin y-coordinate 
r = 1;  % radius of circle 
n = 3;  % number of pieces 

% predefined points -> [x1,x2,xn;y1,y2,yn] 

p = [ 1.5, 2.0, 1.50, 2.4, 1.8, 1.5, 1.7, 2.0, 1.8, 2.5, 2.7, 2.0, 1.3, 1.2, 1.4, 2.2, 1.7, 1.2; 
    0.2, 0.8, 1.20, 1.0, 1.3, 0.8, 1.1, 1.4, 0.7, 0.6, 0.5, 0.4, 1.1, 1.3, 1.5, 1.8, 1.5, 1.1]; 

% calculate circle 

theta = -pi:0.01:pi; 

cirx = r*cos(theta) + x0; 

ciry = r*sin(theta) + y0; 

% initial plot 

figure; 
hold on; 

axis square; 

plot(x0,y0,'or');   % origin 

plot(cirx,ciry);   % circle 

plot(p(1,:),p(2,:),'go'); % predefined points 

% calculate and plot separations 

ciro = linspace(-pi,pi,n+1); 

for k = 1:(numel(ciro)) 

ph(k) = plot([x0,x0+r*cos(ciro(k))],[y0,y0+r*sin(ciro(k))]); 

end 

cevap

1

genel bir yaklaşım içermektedir, her kartezyen (x, y) kutup eşdeğer içine işaret (rho, teta) kökeni kullanarak (dönüştürmek olduğunu x0, y0) merkez olarak. Hangi sektöre ait olduğunu belirlemek için theta değerini kullanabilirsiniz.

% Compute polar coordinates 
[theta, rho] = cart2pol(p(1,:) - x0, p(2,:) - y0); 

% Sector theta values with an extra one at the end for periodicity 
sector_thetas = linspace(-pi,pi,n+1); 

% Identify which sector it falls into by checking theta against one 
% sector and then the next. Should be greater than one and less than the other 
membership = bsxfun(@le, sector_thetas(1:end-1).', theta) & ... 
      bsxfun(@gt, sector_thetas(2:end).', theta); 

% The result is [nRows x nPoints] and each column will contain a 1 in the row 
% corresponding to the sector it belongs to. 

% Get the index of which sector it belongs to 
% This uses the trick here: http://stackoverflow.com/questions/35950922/finding-the-column-index-for-the-1-in-each-row-of-a-matrix/35951036#35951036 
sector = (1:n) * membership; 

farklı sektörleri çizmek durumunda her grup içinde aittir nokta endekslerini bilmek istiyorsanız Sonra, işler uygun

colors = 'grb'; 

for k = 1:n 
    toplot = sector == k; 
    p(k) = plot(p(1, toplot), p(2, toplot), ... 
       'Color', colors(k), ... 
       'Marker', 'o', ... 
       'DisplayName', sprintf('Sector %d', k)); 
end 

legend(p); 

enter image description here

gruplandırıldı sağlamak, sen aşağıdakileri yapabilirdi.

members = arrayfun(@(x)find(sector == x), 1:n, 'uniform', 0); 
celldisp(members) 

    members{1} = 

     1  2  6  9 12 

    members{2} = 

     4 10 11 

    members{3} = 

     3  5  7  8 13 14 15 16 17 18 
+0

Çok teşekkürler ... aynısını istiyorum. – Tina

+0

@ Suever, bir sorum var. Eğer noktayı saymak istersem, 1,2, ... 18 ve çıkış seneri olarak almak istiyorum 1 = {point 1,2,6,9,12} ve sector 2 = {point 4,10,11 } ve sektör 3 = {nokta 3,5, ...}. Kodun hangi kısmını değiştirmem gerekiyor? Aslında çıktıyı bu şekilde göstermek istiyorum. – Tina

+0

@Tina Bu yazıyı nasıl güncellediğimi anlatan bir örnekle – Suever