Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave: compare two vectors

Tags:

octave

I have this vector:

t = 1: 10 % t = 1 2 3 ..10

A= [3 4 5] % a column vector 

If I type:

(3 == t)

I get the result:

0 0 1 0 0 0 0 0 0 0 % it means: 1 at location equals, and 0 at others

I want to do this for vector a, meaning that it will take each element in vector A and compare and return another vector. So in this case, the result will be a 3×10 matrix.

But this line will result in an error: A==t.

Of course, I can do this by using a for loop, but I want to vectorize this operation.

like image 230
hqt Avatar asked Dec 20 '22 17:12

hqt


1 Answers

what you are looking for is the function ismember

octave> t = 1:10
t =
    1    2    3    4    5    6    7    8    9   10

octave> A = ismember (t, [2 3 4])
A =
   0   1   1   1   0   0   0   0   0   0
like image 180
carandraug Avatar answered Jan 15 '23 14:01

carandraug