Question
Matlab code Function Name: onions Inputs: 1. ( cell ) An MxN cell array Outputs: (double ) A 1x2 vector of the position of the
Matlab code
Function Name: onions
Inputs:
1. ( cell ) An MxN cell array
Outputs:
(double ) A 1x2 vector of the position of the element with the most cell array "layers", in the form [row, col]
( double ) The number of "layers" of this element
Background:
Everyone remembers the famous, comical line from Shrek to Donkey in the original Shrek movie: "Onions have layers. Ogres have layers. Cell arrays have layers. You get it? We all have layers." But the question Shrek didn't answer is how many layers these items have. Luckily, you can use your knowledge of MATLAB to count the number of layers in a cell array. We can then use the transitive property to determine how many layers onions and ogres have.
Function Description:
Given an input cell array, write a function that searches through the elements of a cell array. At each position, determine how many layers of cells there are until the base layer (when the next layer is no longer a cell). Your function should output the row and column indices of the element of the cell array with the greatest number of layers, and how many layers this element has.
Example:
>> onionGarden = [ {{6}}, {{{'hello'}}};
{{{{3}}}} , {false} ]
>> [pos, layers] = onions(onionGarden)
pos = [2, 1] layers = 4
Notes:
There will be no ties for the greatest number of layers.
Each cell will always contain either a 1x1 cell or a single non-cell element.
The contents of cells at the deepest layer do not matter, only the number of layers.
Hints:
If you are unsure where to start, look at shallots() .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started