這相當於頭()和尾()在R兩種功能,您可以複製/源代碼粘貼到.m文件並將其保存在您需要的目錄:
function head(data, varargin)
%head - displays the first n rows of an array
%
% Syntax: head(data, n)
%
% Inputs:
% data - data to display (table, double)
% n - number of rows (integer, optional input)
%
% Author: Daniel A. Brodén
% KTH, Royal Institute of Technology, Osquldas Väg 10, 100 44, Stockholm
% email: [email protected]
% Website: http://www.kth.se/profile/danbro
% August 2015; Last revision: Aug-2015
% Define parser object
p = inputParser;
% Required inputs
addRequired(p, 'data')
% Default values
default_n = 10;
% Optional inputs
checkInt = @(x) validateattributes(x,{'numeric'},{'integer','positive'});
addOptional(p, 'n', default_n, checkInt)
% Parse inputs
parse(p, data, varargin{:})
% Conditions
if size(p.Results.data, 1) < p.Results.n
error('Not enough rows')
end
% Return
data(1:p.Results.n,:)
end
代碼尾
function tail(data, varargin)
%tail - displays the last n rows of an array
%
% Syntax: tail(data, n)
%
% Inputs:
% data - data to display (table, double)
% n - number of rows (integer, optional input)
%
% Author: Daniel A. Brodén
% KTH, Royal Institute of Technology, Osquldas Väg 10, 100 44, Stockholm
% email: [email protected]
% Website: http://www.kth.se/profile/danbro
% August 2015; Last revision: Aug-2015
% Parser object
p = inputParser;
% Required inputs
addRequired(p, 'data')
% Default values
default_n = 10;
% Optional inputs
checkInt = @(x) validateattributes(x,{'numeric'},{'integer','positive'});
addOptional(p, 'n', default_n, checkInt)
% Parse inputs
parse(p, data, varargin{:})
% Conditions
if size(p.Results.data, 1) < p.Results.n
error('Not enough rows')
end
% Return
data((size(data,1)-p.Results.n + 1):size(data,1),:)
end
爲什麼不直接自己寫呢?這聽起來像是你用於原型設計的東西,所以如果它只對你的電腦本地的話,這應該不成問題。寫你自己的,並將它添加到你的路徑...... – Dan 2014-10-28 14:55:34
正是,我正在寫同樣的東西。它看起來微不足道的創造。它只是一個代碼行的功能,只是寫它! – 2014-10-28 14:56:18
是的,的確如此。我想它不存在,然後... – Mace 2014-10-28 15:13:25