What does ix=ismembe​r(vars,["s​tate","sou​rce"])| contains(vars,["lat", "lon", "timestamp", "damage", "event"]); do?

What does the code line
ix=ismember(vars,["state","source"])| contains(vars,["lat", "lon", "timestamp", "damage", "event"]);
As far as I understand, the ix of each row is 1 when "state" and "source" or ["lat", "lon", "timestamp", "damage", "event"] are in the vars-string. Is this correct?

1 Comment

Where any element of vars is either:
  • equal to "state"
  • equal to "source"
  • contains "lat"
  • contains "lon"
  • contains "timestamp"
  • etc.
the corresponding element of the output IX will be TRUE, otherwise the element will be FALSE.

Sign in to comment.

 Accepted Answer

idx1=ismember(vars,["state","source"]) ; % gives logical indexing where state, source are presnt in vars
idx2 = contains(vars,["lat", "lon", "timestamp", "damage", "event"]); % gives logical indices where loat, lon etc are present in vats
ix = idx1 | idx2 ; % gives logical indexing of or of the above indices

6 Comments

"presnt in" is ambiguous: does that mean that they must be an exact match, or just match substrings?
Thanks a lot for these answers! But one question: Why are there two functions, which do the same thing?
I mean
ix=ismember(vars,["state","source"])| ismember(vars,["lat", "lon", "timestamp", "damage", "event"]);
would be aquivalent, or where is my mistake?
Second question: According to help vars should be in ["stats", "source"]? Shouldn't this be the opposite?:
ismember(["state","source"],vars)
help: Lia = ismember(A,B) returns an array containing logical 1 (true) where the data in A is found in B. Elsewhere, the array contains logical 0 (false).
contains is used for strings...... you can check the output of each and comapre yourself to find out are they same or not.
"Why are there two functions, which do the same thing?"
They don't do the same thing.
Thanks!
What about my second question? (Sorry, it seems that I have edited to late)

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 13 Jan 2022

Commented:

on 13 Jan 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!