Combining Arrays in MATLAB

Which of the below MATLAB scripts creates a new MATLAB variable called "pets" combining the original arrays so that both variables appear in the new array?

A) pets = [cat; dog]
B) pets = [dog, cat]
C) pets = [cat, dog]
D) pets = [dog; cat]

Final answer:

The correct MATLAB script that creates a new MATLAB variable called "pets" combining the original arrays so that both variables appear in the new array is option C. It uses the comma operator to combine the arrays horizontally.

Explanation:

The correct MATLAB script that creates a new MATLAB variable called "pets" combining the original arrays so that both variables appear in the new array is option C) pets = [cat, dog].

In MATLAB, when combining arrays horizontally, we use the comma ',' operator. So, to combine the arrays 'cat' and 'dog', we use: pets = [cat, dog].

Option A uses a semicolon ';' operator, which concatenates arrays vertically. Option B combines the arrays in the wrong order. Option D uses a semicolon ';' operator but in reverse order, so it will concatenate the arrays in the wrong order. Therefore, option C is the correct choice.

← Speed up your programming skills with these problem solving challenges Selecting options in web ui for worksheets →