2012-09-27 99 views
1

我有一个数据集的粒子。在Matlab中有条件的散点图

第1列是粒子的电荷,第2列是x坐标,第3列是y坐标。

我已将列1 c_particles,列2 x_particles和列3 y_particles重命名。

我需要做一个x vs 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