Difference between two Vectors

16 ビュー (過去 30 日間)
connor
connor 2013 年 11 月 9 日
コメント済み: connor 2013 年 11 月 11 日
I have two arrays
A = [-1 1 -2 1 -1 1 -1 -2 -1 1];
B = [-1 1 1 -1 1 -1 -1 1];
and i am wanting to find all of the values in A that do not appear in B.
So the answer would appear that
values = [-2 -2 ] % values that are in A but not B
index = [3 8] % location of 'values' in A
thanks
  1 件のコメント
connor
connor 2013 年 11 月 11 日
Further to my original question, i now need to find the difference between two vectors of different sizes , but the terms that are new will not be unique ie,
a = [-1 1 2 -2 1 -1 -2 -2 1 -1 -2 2 -1 1]
b = [-1 1 2 -2 1 -1 1 -1 -2 2 -1 1]
hence the difference between these two vectors are the two -2 terms at the centre of a .
The current code will only find new terms that have not appeared before but not the difference in the vectors.
Appreciate any help, thanks.

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 9 日
A = [-1 1 -2 1 -1 1 -1 -2 -1 1];
B = [-1 1 1 -1 1 -1 -1 1];
a=setdiff(A,B)
index=find(ismember(A,a))
values=A(index)
  2 件のコメント
connor
connor 2013 年 11 月 9 日
Thanks
connor
connor 2013 年 11 月 11 日
Further to my original question, i now need to find the difference between two vectors of different sizes , but the terms that are new will not be unique ie,
a = [-1 1 2 -2 1 -1 -2 -2 1 -1 -2 2 -1 1] b = [-1 1 2 -2 1 -1 1 -1 -2 2 -1 1] hence the difference between these two vectors are the two -2 terms at the centre of a .
The current code will only find new terms that have not appeared before but not the difference in the vectors.
Appreciate any help, thanks.

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 11 月 9 日
ii = ~ismember(A,B)
values = A(ii)
index = find(ii)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by