diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fc8463a73..2f2ff2ca7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 1.9.0 (UNRELEASED) + +### Added + +- [#1250](http://github.com/nelson-lang/nelson/issues/1250) `head`, `tail` functions for table and array. + ## 1.8.0 (2024-10-04) ### Added diff --git a/modules/table/functions/head.m b/modules/table/functions/head.m new file mode 100644 index 0000000000..63c3c5a315 --- /dev/null +++ b/modules/table/functions/head.m @@ -0,0 +1,31 @@ +%============================================================================= +% Copyright (c) 2016-present Allan CORNET (Nelson) +%============================================================================= +% This file is part of the Nelson. +%============================================================================= +% LICENCE_BLOCK_BEGIN +% SPDX-License-Identifier: LGPL-3.0-or-later +% LICENCE_BLOCK_END +%============================================================================= +function varargout = head(varargin) + narginchk(1, 2); + nargoutchk(0, 1); + if nargin < 2 + k = 8; + else + k = varargin{2}; + end + A = varargin{1}; + k = min(k, size(A, 1)); + mustBeInteger(k, 2); + mustBeGreaterThanOrEqual(k, 0, 2); + out = A(1:k, :); + + if nargout > 0 + varargout{1} = out; + else + disp(out); + end +end +%============================================================================= + \ No newline at end of file diff --git a/modules/table/functions/tail.m b/modules/table/functions/tail.m new file mode 100644 index 0000000000..aff6932ae3 --- /dev/null +++ b/modules/table/functions/tail.m @@ -0,0 +1,32 @@ +%============================================================================= +% Copyright (c) 2016-present Allan CORNET (Nelson) +%============================================================================= +% This file is part of the Nelson. +%============================================================================= +% LICENCE_BLOCK_BEGIN +% SPDX-License-Identifier: LGPL-3.0-or-later +% LICENCE_BLOCK_END +%============================================================================= +function varargout = tail(varargin) + narginchk(1, 2); + nargoutchk(0, 1); + if nargin < 2 + k = 8; + else + k = varargin{2}; + end + A = varargin{1}; + k = min(k, size(A, 1)); + mustBeInteger(k, 2); + mustBeGreaterThanOrEqual(k, 0, 2); + + out = A(size(A, 1) - k + 1:size(A, 1), :); + + if nargout > 0 + varargout{1} = out; + else + disp(out); + end +end +%============================================================================= + \ No newline at end of file diff --git a/modules/table/help/en_US/xml/head.xml b/modules/table/help/en_US/xml/head.xml new file mode 100644 index 0000000000..bfd03c3a87 --- /dev/null +++ b/modules/table/help/en_US/xml/head.xml @@ -0,0 +1,90 @@ + + + SAME AS NELSON SOFTWARE + + en_US + head + Get top rows of table or array. + + + head(A) + head(A, k) + B = head(...) + + + + + + A + Input array (table or other). + + + + + + + k + a integer value: Number of rows to extract (k = 8 by default). + + + + +

head(A) displays the first eight rows of an array, or table A in the Command Window without assigning it to a variable.

+

head(A, k) displays the first k rows of A.

+

B = head(...) returns the specified rows of A for any of the previous syntaxes, with B having the same data type as A.

+
+ + + + + + + nelson + + + + + + + nelson + + + + + + + + + + tail + + + table + + + + + + + 1.9.0 + initial version + + + + + Allan CORNET + +
diff --git a/modules/table/help/en_US/xml/tail.xml b/modules/table/help/en_US/xml/tail.xml new file mode 100644 index 0000000000..5401bfdb4a --- /dev/null +++ b/modules/table/help/en_US/xml/tail.xml @@ -0,0 +1,90 @@ + + + SAME AS NELSON SOFTWARE + + en_US + tail + Get bottom rows of table or array. + + + tail(A) + tail(A, k) + B = tail(...) + + + + + + A + Input array (table or other). + + + + + + + k + a integer value: Number of rows to extract (k = 8 by default). + + + + +

tail(A) displays the last eight rows of an array, or table A in the Command Window without assigning it to a variable.

+

tail(A, k) displays the last k rows of A.

+

B = tail(...) returns the specified rows of A for any of the previous syntaxes, with B having the same data type as A.

+
+ + + + + + + nelson + + + + + + + nelson + + + + + + + + + + head + + + table + + + + + + + 1.9.0 + initial version + + + + + Allan CORNET + +
diff --git a/modules/table/tests/test_head.m b/modules/table/tests/test_head.m new file mode 100644 index 0000000000..353236fc1e --- /dev/null +++ b/modules/table/tests/test_head.m @@ -0,0 +1,57 @@ +%============================================================================= +% Copyright (c) 2016-present Allan CORNET (Nelson) +%============================================================================= +% This file is part of the Nelson. +%============================================================================= +% LICENCE_BLOCK_BEGIN +% SPDX-License-Identifier: LGPL-3.0-or-later +% LICENCE_BLOCK_END +%============================================================================= +LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'}; +Age = [38;43;38;40;49]; +Smoker = logical([1;0;1;0;1]); +Height = [71;69;64;67;64]; +Weight = [176;163;131;133;119]; +BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80]; +T = table(LastName, Age, Smoker, Height, Weight, BloodPressure); +R = evalc('head(T, 2)'); +REF = ' LastName Age Smoker Height Weight BloodPressure + ___________ ___ ______ ______ ______ _____________ + + {''Sanchez''} 38 true 71 176 124 93 + {''Johnson''} 43 false 69 163 109 77 + +'; +assert_isequal(R, REF); +%============================================================================= +A = repmat((1:100)',1,4); +R = evalc('head(A)'); +REF = ' 1 1 1 1 + 2 2 2 2 + 3 3 3 3 + 4 4 4 4 + 5 5 5 5 + 6 6 6 6 + 7 7 7 7 + 8 8 8 8 + +'; +assert_isequal(R, REF); +%============================================================================= +R = evalc('head(A, 2)'); +REF = ' 1 1 1 1 + 2 2 2 2 + +'; +assert_isequal(R, REF); +%============================================================================= +R = head(A, 8); +REF = repmat((1:8)', 1, 4); +assert_isequal(R, REF); +%============================================================================= +msg = sprintf(_('Invalid input argument at position %d.\nValue must be greater than or equal to %d.'), 2, 0); +assert_checkerror('head(A, -1)', msg, 'Nelson:validators:mustBeGreaterThanOrEqual'); +%============================================================================= +msg = sprintf(_('Invalid input argument at position %d.\nValue must be integer.'), 2); +assert_checkerror('head(A, 1.2)', msg); +%============================================================================= diff --git a/modules/table/tests/test_tail.m b/modules/table/tests/test_tail.m new file mode 100644 index 0000000000..5d14e0e3b9 --- /dev/null +++ b/modules/table/tests/test_tail.m @@ -0,0 +1,34 @@ +%============================================================================= +% Copyright (c) 2016-present Allan CORNET (Nelson) +%============================================================================= +% This file is part of the Nelson. +%============================================================================= +% LICENCE_BLOCK_BEGIN +% SPDX-License-Identifier: LGPL-3.0-or-later +% LICENCE_BLOCK_END +%============================================================================= +LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'}; +Age = [38;43;38;40;49]; +Smoker = logical([1;0;1;0;1]); +Height = [71;69;64;67;64]; +Weight = [176;163;131;133;119]; +BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80]; +T = table(LastName, Age, Smoker, Height, Weight, BloodPressure); +R = evalc('tail(T, 2)'); +REF = ' LastName Age Smoker Height Weight BloodPressure + _________ ___ ______ ______ ______ _____________ + + {''Diaz''} 40 false 67 133 117 75 + {''Brown''} 49 true 64 119 122 80 + +'; +assert_isequal(R, REF); +%============================================================================= +A = repmat((1:100)', 1, 4); +R = evalc('tail(A, 2)'); +REF = ' 99 99 99 99 + 100 100 100 100 + +'; +assert_isequal(R, REF); +%============================================================================= diff --git a/modules/validators/builtin/cpp/mustBeGreaterThanBuiltin.cpp b/modules/validators/builtin/cpp/mustBeGreaterThanBuiltin.cpp index 995a028f0b..82030774f6 100644 --- a/modules/validators/builtin/cpp/mustBeGreaterThanBuiltin.cpp +++ b/modules/validators/builtin/cpp/mustBeGreaterThanBuiltin.cpp @@ -20,7 +20,7 @@ Nelson::ValidatorsGateway::mustBeGreaterThanBuiltin(int nLhs, const ArrayOfVecto { ArrayOfVector retval; nargoutcheck(nLhs, 0, 0); - nargincheck(argIn, 1, 3); + nargincheck(argIn, 2, 3); int argPos = -1; if (argIn.size() == 3) { ArrayOf param3 = argIn[2]; diff --git a/modules/validators/builtin/cpp/mustBeGreaterThanOrEqualBuiltin.cpp b/modules/validators/builtin/cpp/mustBeGreaterThanOrEqualBuiltin.cpp index 6eee05e959..50b7950cc8 100644 --- a/modules/validators/builtin/cpp/mustBeGreaterThanOrEqualBuiltin.cpp +++ b/modules/validators/builtin/cpp/mustBeGreaterThanOrEqualBuiltin.cpp @@ -20,7 +20,7 @@ Nelson::ValidatorsGateway::mustBeGreaterThanOrEqualBuiltin(int nLhs, const Array { ArrayOfVector retval; nargoutcheck(nLhs, 0, 0); - nargincheck(argIn, 1, 3); + nargincheck(argIn, 2, 3); int argPos = -1; if (argIn.size() == 3) { ArrayOf param3 = argIn[2]; diff --git a/modules/validators/builtin/cpp/mustBeLessThanBuiltin.cpp b/modules/validators/builtin/cpp/mustBeLessThanBuiltin.cpp index 8685444425..adce7c0f21 100644 --- a/modules/validators/builtin/cpp/mustBeLessThanBuiltin.cpp +++ b/modules/validators/builtin/cpp/mustBeLessThanBuiltin.cpp @@ -20,7 +20,7 @@ Nelson::ValidatorsGateway::mustBeLessThanBuiltin(int nLhs, const ArrayOfVector& { ArrayOfVector retval; nargoutcheck(nLhs, 0, 0); - nargincheck(argIn, 1, 3); + nargincheck(argIn, 2, 3); int argPos = -1; if (argIn.size() == 3) { ArrayOf param3 = argIn[2]; diff --git a/modules/validators/builtin/cpp/mustBeLessThanOrEqualBuiltin.cpp b/modules/validators/builtin/cpp/mustBeLessThanOrEqualBuiltin.cpp index ef7a83298b..f2374c3c8a 100644 --- a/modules/validators/builtin/cpp/mustBeLessThanOrEqualBuiltin.cpp +++ b/modules/validators/builtin/cpp/mustBeLessThanOrEqualBuiltin.cpp @@ -20,7 +20,7 @@ Nelson::ValidatorsGateway::mustBeLessThanOrEqualBuiltin(int nLhs, const ArrayOfV { ArrayOfVector retval; nargoutcheck(nLhs, 0, 0); - nargincheck(argIn, 1, 3); + nargincheck(argIn, 2, 3); int argPos = -1; if (argIn.size() == 3) { ArrayOf param3 = argIn[2];