create a function to find if two vectors have equal entries in each row in matlab

You can create a function that takes in two vectors and checks if they have equal entries in each row, like so:

main.m
function equal_rows = vector_equal_rows(vec1, vec2)
    equal_rows = isequal(vec1, vec2);
end
94 chars
4 lines

The isequal function compares the two vectors element-wise and returns a logical value indicating if the vectors have the same size and elements or not. The equal_rows variable will be true if the two vectors are equal, and false otherwise.

You can test this function with some example vectors, like this:

main.m
>> vec1 = [1 3 2];
>> vec2 = [1 4 2];
>> equal_rows = vector_equal_rows(vec1, vec2)
equal_rows =
  logical
   0

>> vec3 = [1 3 2];
>> equal_rows = vector_equal_rows(vec1, vec3)
equal_rows =
  logical
   1
206 chars
13 lines

This function will work for vectors of any size (as long as they have the same number of elements), and can be used in other MATLAB code as needed.

related categories

gistlibby LogSnag