danjoro
danjoro

Reputation: 111

Write for logical value Matlab

Let's say T=1:20 ; P=[2 6 9 11 15 19].

How to write a logical value for P in range T?

The answer I want is: flag= [0 1 0 0 0 1 0 0 1 0 1 0 0 0 1 0 0 0 1 0].

Upvotes: 1

Views: 92

Answers (3)

Divakar
Divakar

Reputation: 221614

Use ismember made for exactly this task -

ismember(T,P)

Upvotes: 5

Stewie Griffin
Stewie Griffin

Reputation: 14939

For the fun of it, an alternative to Hoki's answer:

T(P) = 0;
flag = ~T

This sets all values that are in P equal to zero, and then checks if the values in T is 0 or not. This of course has the downside that it overwrites T. Note: I would go for Hoki's answer!

Upvotes: 2

Hoki
Hoki

Reputation: 11812

You can define a logical vector flag the size of T, then use P as an index vector of the flag to raise to true:

T=1:20 ; P=[2 6 9 11 15 19] ;

flag = false(size(T)) ;
flag(P) = true ;

flag =
     0     1     0     0     0     1     0     0     1     0     1     0     0     0     1     0     0     0     1     0

Upvotes: 4

Related Questions