Creating an array from 2 other arrays

I need a code that will do the following: Given an array X and another array Y which is smaller than X and every element in Y exists in X, I need to create a third array Z which consists of every element that is in X and NOT in Y.
For example, if X = [1 2 3 4 5] and Y = [1 2 3] then I want the code to return an array Z = [4 5].
Can anyone help write this code for me? I've been trying to do it using the 'find' function but can't get it to work. Thanks for the help.

 채택된 답변

Matt Fig
Matt Fig 2011년 2월 16일

0 개 추천

Z = setdiff(X,Y)
or (just for fun):
Z = X(~ismember(X,Y))

댓글 수: 1

Ute123
Ute123 2011년 2월 16일
Didn't know that function existed. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2011년 2월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by