matching unequal arrays

Hi; I want to match the values of unequal arrays for a case as
m=(1:6); n=[1 2 5];
output=(0 0 1 1 0 1)
that is if an element of n is in m then the value in output is zero else 1.
Any hints?

 Accepted Answer

output = ~ismember(m,n)

3 Comments

Sara
Sara on 9 Jan 2012
if n is 2x3 whereas m is 1x6 is it still possible to assign the values in row 2 of n to output
say n=[1 2 5; 2 3 4];
output is then [0 0 1 1 0 1; 0 0 2 3 0 4]
n=[1 2 5; 2 3 4];
m = 1:6;
output = zeros(size(m) + [1 0]);
t = ~ismember(m,n(1,:));
output(1,:) = t;
output(2,t) = n(2,:)
Sara
Sara on 10 Jan 2012
When I use the code on arrays of different size for instance m=1x1502 and n=2x866 there will be a mismatch in the last row of the code, obviously the size at the left and right of the array is not equal. Would n(2,: ) be expanded to the appropriate size without using loops?

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!