2

I would like to see if an array starts with the same elements as another array without having to write a bunch of for loops going through each element individually.

For example if I had the arrays below

Array1 = [1 2 3 4]
Array2 = [1 2 3 4 5 3 2 5 7]

Array3 = [1 2 3 5]

Then comparing Array1 with Array2 would return true. and comparing Array3 with Array2 would return false.

Is there any quick and easy way of doing this. I would not know the lengths of the arrays I would be comparing. The number of elements I want to compare equals the length of the shortest vector.

Thanks!

1
  • 2
    How many of the first elements must be equal? Commented Jul 15, 2014 at 7:16

4 Answers 4

3

You can check if all elements in two vectors are the same using isequal. To check only the first n elements, you can do Array(1:n), thus the entire function will be like this:

Array1 = [1 2 3 4]
Array2 = [1 2 3 4 5 3 2 5 7]
Array3 = [1 2 3 5]

n = 4;    % Compare the first n elements
isequal(Array1(1:n), Array2(1:n))
ans =  1

isequal(Array2(1:n), Array3(1:n))
ans = 0

If you use Array1(1:n) == Array2(1:n) you will get a piece-wise comparison resulting in 1 1 1 1. Of course, this means you could also do:

all(Array1(1:n) == Array2(1:n))
ans =  1

all(Array2(1:n) == Array3(1:n))
ans =  0

If you want n to be the number of elements in the smallest vector (per your comment), as Chris and Ben interpret the question, you can solve it this way:

isequal(Array1(min([numel(Array1) numel(Array2)])), Array2(min([numel(Array1) numel(Array2)])))

or a bit cleaner:

n = min([numel(Array1) numel(Array2)])
isequal(Array1(1:n), Array2(1:n))
Sign up to request clarification or add additional context in comments.

Comments

2

Here's a function that will compare the initial segments of any two vectors, up to the length of the shortest vector. It returns true if they are identical, and false if they are not identical.

Note that

  1. It only works correctly with vectors, not with matrices (although you could extend it to deal with matrices)
  2. If any entries are NaN then it will always return false, since NaN == NaN is false.

Here it is -

function result = equal_initial_segment(x, y)

  N = min(length(x), length(y));

  result = isequal(x(1:N), y(1:N));

end

Comments

0

It seems like you are just comparing the all of the elements of the shorter list to the first elements of the longer list, in which case you can just do this:

function same = compareLists(list1, list2)
    if length(list1) > length(list2)
         same = isequal(list2, list1(1:length(list2));
    else if
         same = isequal(list1, list2(1:length(list1));
    end
end

Comments

0

You can use strmatch for that:

~(isempty(strmatch(Array1, Array2)) && isempty(strmatch(Array2, Array1)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.