Using Matlab Function Name: countType Inputs: 1. (cell) A 1xM cell array Outputs
ID: 663581 • Letter: U
Question
Using Matlab
Function Name: countType
Inputs:
1. (cell) A 1xM cell array
Outputs:
1. (cell) A 2x4 cell array describing the contents of the input cell array
Function Description:
Each cell in a cell array may contain any type of data structure. Given a 1xM cell array, write a function, countType, which will find and count the class of each cell in the cell array and output a 2x4 cell array. The first row of the cell array will consist of the possible classes that could be counted—‘double’, ‘char’, ‘logical’, or ‘cell’, in that order —and the second row of the cell array will give the number of each type of class found in the input cell array.
For example, if the input 1x7 cell array were ca below, the output cell array would be caNew. This shows the input cell array had 2 cells containing doubles, 1 cell containing a char, 2 cells containing logicals, and 2 cells containing cell arrays.
ca = {{‘cell’}, [9 3 5], 3, true, [false true], ‘MATLAB’, {9}}; caNew = countType(ca)
Note:
? The cell arrays are given such that if you entered them into the command window, they would give you the proper answer. The result is not supposed to appear in command window as given above.
? The first row of the output cell array will always contain the classes of the cells in the order given above.
Explanation / Answer
function caNew = countType(ca)
caNew = {'double', 'char', 'logical', 'cell'; 0 0 0 0};
for i = 1:size(ca)
obj = class(ca{1,i});
if (obj == 'char')
caNew{2,2} = caNew{2,2} + 1;
else if (obj == 'logical')
caNew{2,3} = caNew{2,3} + 1;
else if (obj == 'cell')
caNew{2,4} = caNew{2,4} + 1;
else
caNew{2,1} = caNew{2,1} + 1;
end
end
caNew;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.