% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % data_standardised = standardise_to(data, mean_to_scale, std_to_scale) %% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % FUNCTIONALITY: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This function aim is to standardise a data set (X) using the mean %%%%%% % (mean_to_scale) and the standard deviation (std_to_scale) provided by %% % the user. This method is intended for bidimensional data.%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % INPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % data: Data set to standardise. It can be either a vector or % % a matrix. The number of columns in data must be the %%% % same as the number of elements in the mean and %%%%%%%% % standard deviation values given. %%%%%%%%%%%%%%%%%%%%%% % mean_to_scale: Vector that contains the mean values for each %%%%%%%%% % variable (columns in data). The number of elements in % % this vector must be the same as the number of columns % % in data. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % std_to_scale: Vector that contains the standard deviations to use %%% % to scale the data set (data). The number of elements %% % in this vector must be the same that the number of %%%% % columns in the data set (data). %%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % OUTPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % data_standardised: Data set (data) after standardised. It has the %%%% % same dimensions that data (vector or matrix). If %% % any problem occurred while standardising data, %%%% % the returned value is NaN. Otherwise, the %%%%%%%%% % returned values are doubles. %%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % INITIALISATIONS: First of all we have to preallocate the memory required to standardise the data. To do so, we have to check if the dimensions of the mean and standard deviation vectors are equal to the number of columns in data.
0001 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0002 %% data_standardised = standardise_to(data, mean_to_scale, std_to_scale) %% 0003 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0004 %% FUNCTIONALITY: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0006 %% This function aim is to standardise a data set (X) using the mean %%%%%% 0007 %% (mean_to_scale) and the standard deviation (std_to_scale) provided by %% 0008 %% the user. This method is intended for bidimensional data.%%%%%%%%%%%%%%% 0009 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0010 %% INPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0011 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0012 %% data: Data set to standardise. It can be either a vector or % 0013 %% a matrix. The number of columns in data must be the %%% 0014 %% same as the number of elements in the mean and %%%%%%%% 0015 %% standard deviation values given. %%%%%%%%%%%%%%%%%%%%%% 0016 %% mean_to_scale: Vector that contains the mean values for each %%%%%%%%% 0017 %% variable (columns in data). The number of elements in % 0018 %% this vector must be the same as the number of columns % 0019 %% in data. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0020 %% std_to_scale: Vector that contains the standard deviations to use %%% 0021 %% to scale the data set (data). The number of elements %% 0022 %% in this vector must be the same that the number of %%%% 0023 %% columns in the data set (data). %%%%%%%%%%%%%%%%%%%%%%% 0024 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0025 %% OUTPUT PARAMETERS: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0026 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0027 %% data_standardised: Data set (data) after standardised. It has the %%%% 0028 %% same dimensions that data (vector or matrix). If %% 0029 %% any problem occurred while standardising data, %%%% 0030 %% the returned value is NaN. Otherwise, the %%%%%%%%% 0031 %% returned values are doubles. %%%%%%%%%%%%%%%%%%%%%% 0032 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0033 function data_standardised = standardise_to(data, mean_to_scale, std_to_scale) 0034 %% INITIALISATIONS: 0035 % First of all we have to preallocate the memory required to standardise 0036 % the data. To do so, we have to check if the dimensions of the mean and 0037 % standard deviation vectors are equal to the number of columns in data. 0038 if size(data,2) == length(mean_to_scale) && size(data,2) == length(std_to_scale) 0039 % - All variables have the same number of variables, so we can proceed 0040 % normally. 0041 0042 % -- Now, we standardise the data using the mean and vectors the user 0043 % passed as input parameters. 0044 data_standardised = (data - repmat(mean_to_scale, size(data, 1), 1)) ./ repmat(std_to_scale, size(data, 1), 1); 0045 else 0046 % - Some elements do not have the same dimensions. As a consequence, we 0047 % will return an error by initialising the data to standardise as a 0048 % NaN. 0049 data_standardised = nan(); 0050 end