%% New in r2020b: new way to return height or width of arrays
% Created for Matlab Central Community Highlights, by Adam Danz on Oct 6, 2020.
%
% Prior to r2020b the height (number of rows) and width (number of columns) of an array or table can be determined by the size function,

array = rand(102, 16);

% Method 1
[dimensions] = size(array);
h = dimensions(1);
w = dimensions(2);

% Method 2
[h, w] = size(array); %#ok<*ASGLU>
% or
[h, ~] = size(array);
[~, w] = size(array);

% Method 3
h = size(array,1);
w = size(array,2);

% In r2013b, the height(T) and width(T) functions were introduced to return the size of single dimensions for tables and timetables.
% Starting in r2020b, height() and width() can be applied to arrays as an alternative to the size() function.
% Continuing from the section above,

h = height(array)
w = width(array)

% height() and width() can also be applied to multidimensional arrays including cell and structure arrays

mdarray = rand(4,3,20);
h = height(mdarray)
w = width(mdarray)

% The expanded support of the height() and width() functions means,
% when reading code, you can no longer assume the variable T in height(T) refers to a table or timetable
% greater flexibility in expressions such as the these, below

% C is a 1x4 cell array containing 4 matrices with different dimensions
rng('default')
C = {rand(5,2), rand(2,3), rand(3,4), rand(1,1)};
celldisp(C)

% What's the max number of rows in C?

maxRows1 = max(cellfun(@height,C))         % using height()
maxRows2 = max(cellfun(@(x)size(x,1),C))   % using size()

% What's the total number of columns in C?

totCols1 = sum(cellfun(@width,C))          % using width()
totCols2 = sum(cellfun(@(x)size(x,2),C))   % using size(x,2)