for people wondering how i fixed the problem by the answer of Walter Roberson:
made the compution much faster as well!
figure(1)
count_green=1; %counters to construct the three subsets from position 1 to x
count_red=1;
count_black=1;
for j=1:16
for i=(1+((j-1)*61)):(30+((j-1)*61))
if CPN(i) < -1 && CPN(i) > -2 % stores all values which meet the condition in their corresponding subset
CPN_red(count_red)=CPN(i);
x_red(count_red)=x(i);
y_red(count_red)=y(i);
z_red(count_red)=z(i);
count_red=count_red+1;
elseif CPN(i) < -2
CPN_black(count_black)=CPN(i); % stores all values which meet the condition in their corresponding subset
x_black(count_black)=x(i);
y_black(count_black)=y(i);
z_black(count_black)=z(i);
count_black=count_black+1;
else
CPN_green(count_green)=CPN(i); % stores all values which meet the condition in their corresponding subset
x_green(count_green)=x(i);
y_green(count_green)=y(i);
z_green(count_green)=z(i);
count_green=count_green+1;
end
end
end
figure(1) % plot the three subsets in the same figure
scatter3(x_red,y_red,z_red,150,'filled','red')
hold on
scatter3(x_black,y_black,z_black,250,'filled','black')
hold on
scatter3(x_green,y_green,z_green,80,'filled','green')
legend('2>-C_{PN}>1','-C_{PN}>2','-C_{PN}<1') % legend is filled correctly now!