aspasia
aspasia

Reputation: 179

compare cell arrays a and b, return indices of elements from a not in b, Matlab

I tried to summarize in the title.

I have two cell arrays (with strings) that are similar, except that A has more elements than B. I want to find the indices of those elements from A that are not in B. A and B have the elements ordered in the same way, except that A has those extra interpolated at random intervals.

In Python I would do something like this:

A = ["a", "b", "c", "d", "e", "f"]
B = ["a","c", "d", "f"]

indices = []

for i in A:
  if i not in B:
    indices.append(A.index(i))

Unfortunately, I need it for data in Matlab, for cell arrays that are a bit over 100k long.

Thanks!

Upvotes: 1

Views: 148

Answers (1)

arrey
arrey

Reputation: 232

setdiff should work for this.

[C, ia] = setdiff(A,B);

Returns the data in A that is not in B (ia is the index vector), and it works for cell arrays of strings. Here is the documentation:

http://www.mathworks.com/help/matlab/ref/setdiff.html

Upvotes: 5

Related Questions