Write a function that takes a row vector of numbers and returns a structure with basic summary information.
| name | size | type | description |
|---|---|---|---|
| inputs: | 1xN |
double |
row vector of numbers |
| outputs: | 1x1 |
struct |
structure with fields n, m, M, sorted
|
-
n(int): number of values -
m(double): minimum value -
M(double): maximum value -
sorted(logical):true (1)if the list is nondecreasing; otherwisefalse (0)
function list_info = get_list_info(list)
% set n
n = length(list);
% initialize m, M, & sorted
m = list(1);
M = list(1);
sorted = true;
% determine m, M, & sorted
for k = 2:length(list)
% update m?
if list(k) < m
m = list(k);
end
% update M?
if list(k) > M
M = list(k);
end
% update sorted?
if list(k-1) > list(k)
sorted = false;
end
end
% create the list_info structure
list_info.n = n;
list_info.m = m;
list_info.M = M;
list_info.sorted = sorted;
end
Test the function.
list_info_1 = get_list_info([-11.02 5.3 -2.0 5.3 4.09 5.3])
list_info_2 = get_list_info([1 1 1 1 1 1 1 1])

