Search code examples
matlabmatrixoctavescilab

How do I extract the odd and even rows of my matrix into two separate matrices in scilab?


I'm very new to scilab syntax and can't seem to find a way to extract the even and odd elements of a matrix into two separate matrix, suppose there's a matrix a:

a=[1,2,3,4,5,6,7,8,9]

How do I make two other matrix b and c which will be like b=[2 4 6 8] and c=[1 3 5 7 9]


Solution

  • a=[1,2,3,4,5,6,7,8,9];
    b = a(mod(a,2)==0);
    c = a(mod(a,2)==1);
    
    b =
         2     4     6     8
    c =
         1     3     5     7     9
    

    Use mod to check whether the number is divisible by 2 or not (i.e. is it even) and use that as a logical index into a.