Bird
0
0

Which MATLAB code correctly returns a logical array indicating matches?

hard📝 Application Q15 of 15
MATLAB - String Handling
You want to compare two cell arrays of strings C1 = {'Apple', 'Banana', 'Cherry'} and C2 = {'apple', 'banana', 'cherry'} ignoring case. Which MATLAB code correctly returns a logical array indicating matches?
Acellfun(@strcmpi, C1, C2)
Bstrcmpi(C1{:}, C2{:})
Ccellfun(@strcmp, C1, C2)
Dstrcmp(C1, C2)
Step-by-Step Solution
Solution:
  1. Step 1: Understand cell array string comparison

    To compare each pair of strings in cell arrays element-wise, use cellfun with a comparison function.
  2. Step 2: Choose case-insensitive comparison

    strcmpi ignores case, so cellfun(@strcmpi, C1, C2) applies it element-wise correctly.
  3. Step 3: Why other options fail

    cellfun(@strcmp, C1, C2) uses case-sensitive strcmp. strcmpi(C1{:}, C2{:}) unpacks cells into multiple arguments, causing a "too many input arguments" error. strcmp(C1, C2) is case-sensitive.
  4. Final Answer:

    cellfun(@strcmpi, C1, C2) -> Option A
  5. Quick Check:

    Use cellfun with strcmpi for cell array case-insensitive comparison [OK]
Quick Trick: Use cellfun(@strcmpi, C1, C2) for cell array case-insensitive compare [OK]
Common Mistakes:
  • Using strcmp instead of strcmpi for ignoring case
  • Unpacking cell arrays with strcmpi(C1{:}, C2{:})
  • Not using cellfun for element-wise comparison

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes