Comparing Two Cell Arrays of Strings of Different Sizes in MATLAB


In this article, we will learn how to compare two cell arrays of strings of different sizes in MATLAB.

Cell Array of Strings

In MATLAB, a cell array of strings is a data structure that can store different types of elements. The element of a cell array are enclosed within curly braces `{}`.

Syntax

We can use the following syntax to create a cell array of strings,

A = {'string1', 'string2', 'string3',…'stringN'};

Here, `A` is a cell array of strings that contains N string elements namely, `string1`, `string2`, …, `stringN`.

In MATLAB, the cell arrays of strings are used either when we need to store and manipulate groups of strings of different lengths or when we want to operate with multiple strings at the same time.

Compare Two Cell Arrays of Strings of Different Sizes

MATLAB provides several different methods to compare two cell arrays of strings of different sizes. In the following sections of this article, some important methods to compare two cell arrays of strings of different sizes using MATLAB are explained with the help of MATLAB programs.

Compare Two Cell Arrays of Strings of Different Sizes using `ismember` Function

We can use MATLAB’s built−in function `ismember` to compare two cell arrays. The `ismemeber` function compares two cell arrays and returns a logical array that indicates whether each element of the first cell array is a member of the second cell array.

Syntax

To compare two cell arrays, we can use the following syntax,

C = ismember(A, B);

The following MATLAB program demonstrate the implementation of `ismember` function to compare two cell arrays of strings of different sizes.

Example

% MATLAB program to demonstrate use of `ismember` function to compare two cell arrays of strings of different sizes
% Create two cell arrays of strings
x = {'Tutorials', 'Point', 'Online', 'Library'};
y = {'Tutorials', 'Point', 'is', 'a', 'Digital', 'Platform'};
% Compare arrays `x` and `y` using `ismember` function
A = ismember(x, y)
% Find the set different between two cell arrays using `setdiff` function
B = setdiff(x, y)

Output

A =

  1x4 logical array

   1   1   0   0


B =

  1x2 cell array

    {'Library'}    {'Online'}

Explanation

In this MATLAB program, we start by creating two cell arrays of strings of different sizes and store them in `x` and `y`. Next, we compare the two arrays by using the `ismember` function. This `ismember` function gives a logical array as an output that indicates which elements of the array `x` are present in the array `y`. Then, we call the `setdiff` function to display the elements that are present in the array `x` but not in the array `y`.

Compare Two Cell Arrays of Strings using Looping Approach

We can also compare two cell arrays using a loop. The following MATLAB program demonstrates this implementation.

Example

% MATLAB program to compare two cell arrays using a loop
% Create two cell arrays of strings
x = {'Tutorials', 'Point', 'Online', 'Library'};
y = {'Tutorials', 'Point', 'is', 'a', 'Digital', 'Platform'};
% Run a loop to compare the corresponding elements of two arrays
for i = 1:min(numel(x), numel(y))
    if strcmp(x{i}, y{i})
        disp(['Elements at index ', num2str(i), ' are the same.']);
    else
        disp(['Elements at index ', num2str(i), ' are different.']);
    end
end

Output

Elements at index 1 are the same.
Elements at index 2 are the same.
Elements at index 3 are different.
Elements at index 4 are different.

Explanation

In this MATLAB program, we start by creating two cell arrays `x` and `y` containing strings as their elements. Next, we run a `for` loop to compare the corresponding elements of the two arrays. If the strings in the two cell arrays at a specific index are equal, the code displays a message “Elements at index 'Index Number' are the same” by using the `disp` function. If the strings are different at the index, then the code displays a message “Elements at index 'Index Number' are the different” by using the `disp` function.

Hence, this MATLAB program compares the corresponding elements of the two cell arrays `x` and `y` and displays a message for each index which indicates whether the strings at the current index are the same or different.

Conclusion

Hence, this is how we can compare two cell arrays of strings of different sizes in MATLAB. In the above sections of this article, we have illustrated two most commonly used methods to compare two cell arrays of strings of different sizes with the help of example programs.

Updated on: 07-Aug-2023

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements