Challenge - 5 Problems
Master of MATLAB Type Conversion
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of converting numeric array to logical
What is the output of this MATLAB code?
a = [0 1 2 -3 0];
b = logical(a);
disp(b);
MATLAB
a = [0 1 2 -3 0]; b = logical(a); disp(b);
Attempts:
2 left
💡 Hint
Logical conversion treats zero as false and nonzero as true.
✗ Incorrect
In MATLAB, logical conversion changes zeros to false and any nonzero number to true. Displaying a logical array shows 'false' and 'true'.
❓ Predict Output
intermediate2:00remaining
Result of char to double conversion
What is the output of this MATLAB code?
c = 'MATLAB';
d = double(c);
disp(d);
MATLAB
c = 'MATLAB';
d = double(c);
disp(d);Attempts:
2 left
💡 Hint
double converts characters to their ASCII numeric codes.
✗ Incorrect
The double function converts each character to its ASCII code. 'M' is 77, 'A' is 65, etc.
❓ Predict Output
advanced2:00remaining
Output of converting numeric to string and back
What is the value of variable
y after running this MATLAB code?x = 123.45;
s = string(x);
y = double(s);
MATLAB
x = 123.45;
s = string(x);
y = double(s);Attempts:
2 left
💡 Hint
double applied to a string array returns NaN if string is not numeric array.
✗ Incorrect
The string '123.45' cannot be directly converted to a numeric double array by double(). Instead, double(string) returns NaN. To convert string to number, use str2double.
❓ Predict Output
advanced2:00remaining
Effect of converting logical array to double
What is the output of this MATLAB code?
l = [true false true];
d = double(l);
disp(d);
MATLAB
l = [true false true]; d = double(l); disp(d);
Attempts:
2 left
💡 Hint
Logical true converts to 1, false to 0 when cast to double.
✗ Incorrect
Converting logical true to double gives 1, false gives 0. So the output is [1 0 1].
❓ Predict Output
expert2:00remaining
Output of converting cell array of mixed types to numeric array
What error or output does this MATLAB code produce?
c = {1, '2', 3};
a = cell2mat(c);MATLAB
c = {1, '2', 3};
a = cell2mat(c);Attempts:
2 left
💡 Hint
cell2mat requires all cells to be numeric arrays of compatible sizes.
✗ Incorrect
cell2mat fails if the cell array contains mixed types like numeric and char. It throws an error.