Write a function named cellParse that takes in a cell array with each element be
ID: 3531296 • Letter: W
Question
Write a function named cellParse that takes in a cell array with each element being either a string
(character array), or a vector (containing numbers), or a Boolean value (logical array of length 1).
Your function should return the following:
Explanation / Answer
function [numStr numVec numBool alpha avgLen allTrue] = cellParse(inCA) numStr = 0; numVec = 0; numBool = 0; alpha = {}; avgLen = 0; allTrue = true; for cur = inCA % direct access cur = cellUnwrap(cur); % unwrap, incase cell has embedded cells if ischar(cur) numStr = numStr + 1; % increment string counter alpha = [ alpha {cur} ]; % add string to cell array elseif isnumeric(cur) numVec = numVec + 1; % increment vector counter avgLen = avgLen + length(cur); % add length of vec to total elseif islogical(cur) numBool = numBool + 1; % increment boolean counter if ~cur % if cur is false, return boolean should be false allTrue = false; end end end alpha = sort(alpha); % sort strings if numVec > 0 avgLen = avgLen / numVec; % calc avg length of vectorss end end ---------------------------------------------------------------------------------------------------function innerData = cellUnwrap(inCell) innerData = inCell; % keep accessing first cell until there is no more inner data or until % the item you accessed is not a cell while ~isempty(innerData) && iscell(innerData) innerData = innerData{1}; end % handle special case where innermost cell is empty if iscell(innerData) innerData = []; end end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.