Friday, December 9, 2011

Binary Operation on an Array in MATLAB

Let's imagine that I have the following array (double type). I want to run a binary operation to pick only those columns that are TRUE according to another binary(logical) array :-)


>> A=[1:2:20]
A =
     1     3     5     7     9    11    13    15    17    19

I want to run a binary operation on this array based on the following array Binary_B
>> Binary_B=[ 1 0 1 1 1 0 0 1 1 0]

Right now, Binary_B is also double type. So to convert it into binary format we have use the command "logical"

>>Binary_B=logical(Binary_B)

Now just run the following command to pick only the columns of A where the binary value is 1. I have found this simple command very useful.


>> A(1,Binary_B)

ans =

     1     5     7     9    15    17





No comments:

Post a Comment

Please ask if anything is not clear enough..........