2012-09-27 3 views
1

데이터 집합이 있습니다.Matlab에서의 산포도

열 1은 입자의 전하, 열 2는 x 좌표, 열 3은 y 좌표입니다.

열 1의 c_ 입자, 열 2의 x_ 입자 및 열 3의 y_ 입자라는 이름이 바뀌 었습니다.

x와 y의 산점도를 작성해야하지만 충전량이 양수이면 마커가 빨간색이어야하며 충전량이 음수이면 마커가 파란색이어야합니다.

은 지금까지 나는 음모를 산출한다

if c_particles < 0 
scatter(x_particles,y_particles,5,[0 0 1], 'filled') 
else 
scatter(x_particles,y_particles,5,[1 0 0], 'filled') 
end 

이 있지만 마커는 모두 빨간색입니다.

답변

3

첫 번째 라인은 당신이 생각하는 일을하지 않습니다 :

c_particles < 0 

c_particles과 같은 길이의 논리 값의 벡터를 반환합니다; if는 적어도 하나의 요소가 참일 경우이 배열을 true로 처리합니다. 대신,이 '논리 배열'을 사용하여 플로팅하려는 입자를 인덱싱 할 수 있습니다. 나는 이것을 시도 할 것이다 :

i_negative = (c_particles < 0);  % logical index of negative particles 
i_positive = ~i_negative;    % invert to get positive particles 
x_negative = x_particles(i_negative); % select x-coords of negative particles 
x_positive = x_particles(i_positive); % select x-coords of positive particles 
y_negative = y_particles(i_negative); 
y_positive = y_particles(i_positive); 

scatter(x_negative, y_negative, 5, [0 0 1], 'filled'); 
hold on; % do the next plot overlaid on the current plot 
scatter(x_positive, y_positive, 5, [1 0 0], 'filled'); 
+0

고마워요! 그것은 효과가있다! – Erica