MATLAB: Sorting integers into two arrays based on positive or negative.

arrayfor loopsort

I'm having trouble sorting all the integers into the two separate arrays. I can only recall the last positive and last negative numbers in the arrays.
This is what i have so far.
H = randi([-20,20],1,16);
for array = H;
if array<0
H1 = array;
elseif array>=0
H2 = array;
end
end
H1
H2
when the program runs this is the output.
H1 =
-3
H2 =
14
I'm fairly new to the matlab program so any help is appreciated.

Best Answer

I would do it with logical indexing:
H = randi([-20,20],1,16)
H1 = H(H < 0)
H2 = H(H >= 0)
H =
6 -14 -16 0 19 -7 3 -11 10 -10 0 8 16 19 2 -15
H1 =
-14 -16 -7 -11 -10 -15
H2 =
6 0 19 3 10 0 8 16 19 2