0% found this document useful (0 votes)
13 views10 pages

MATLAB CODE FOR DATA TYPES.

The document provides MATLAB code examples for various data imputation techniques, including mean imputation, linear interpolation, and nearest neighbor methods for handling missing values in datasets. It also includes methods for identifying missing values (NaNs), counting them, and finding empty rows and columns. Additionally, the document discusses checking for data uniqueness and finding duplicate values within a dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

MATLAB CODE FOR DATA TYPES.

The document provides MATLAB code examples for various data imputation techniques, including mean imputation, linear interpolation, and nearest neighbor methods for handling missing values in datasets. It also includes methods for identifying missing values (NaNs), counting them, and finding empty rows and columns. Additionally, the document discusses checking for data uniqueness and finding duplicate values within a dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

MATLAB CODE FOR DATA IMPULATION

It looks like you’re looking for MATLAB code for data imputation—which is
used to fill in missing values in a dataset. Below is a simple example of
different imputation techniques in MATLAB:

MATLAB Code for Data Imputation:

% Sample dataset with missing values (NaN)

Data = [1 2 NaN 4 5;

6 NaN 8 9 10;

11 12 13 NaN 15];

Method 1: Fill missing values with the mean of each column

Data_mean_imputed = data;

For col = 1:size(data,2)

colMean = mean(data(~isnan(data(:,col)), col)); % Mean without NaN

data_mean_imputed(isnan(data_mean_imputed(:,col)), col) = colMean;

end

disp(‘Data after Mean Imputation:’);

disp(data_mean_imputed);

Method 2: Fill missing values using linear interpolation

Data_interp = data;

For col = 1:size(data,2)

Data_interp(:,col) = fillmissing(data(:,col), ‘linear’);

End

Disp(‘Data after Linear Interpolation:’);

Disp(data_interp);
Method 3: Fill missing values with nearest neighbor method

Data_nearest = fillmissing(data, ‘nearest’);

Disp(‘Data after Nearest Neighbor Imputation:’);

Disp(data_nearest);

Explanation of Methods:

1. Mean Imputation: Replaces NaN values with the column mean.


2. Linear Interpolation: Estimates missing values using linear
interpolation.
3. Nearest Neighbor Imputation: Uses the nearest available value to fill
missing values.
MATALAB CODE FOR DATA MISSING VALUE
FINDING

If you want to find missing values (NaNs) in MATLAB, here’s a simple way to
do it:

MATLAB Code to Find Missing Values (NaNs)

Sample dataset with missing values (NaN)

Data = [1 2 NaN 4 5;

6 NaN 8 9 10;

11 12 13 NaN 15];

Find missing values (NaN)

1.Missing_values = isnan(data);

Display missing values matrix

Disp(‘Missing Values (NaN Locations):’);

Disp(missing_values);

2.Count total number of missing values

Num_missing = sum(missing_values();

Disp([‘Total Number of Missing Values: ‘, num2str(num_missing)]);

3.Find row and column indices of missing values

[row, col] = find(isnan(data));

Disp(‘Missing Value Locations (Row, Column):’);

Disp([row, col]);
Explanation:

1. Isnan(data) → Creates a logical matrix where 1 (true) indicates missing


values.
2. Sum(missing_values() → Counts total missing values.
3. Find(isnan(data)) → Finds row and column indices of missing values.

MATALAB CODE FOR DATA EMPTINESS FINDING

If you want to find empty (completely missing) rows or columns in a dataset


using MATLAB, you can use the following approach:

MATLAB Code to Find Empty Rows and Columns

1.Sample dataset with missing values (NaN)

Data = [NaN NaN NaN 4 5;

6 NaN 8 9 10;

NaN NaN NaN NaN NaN;

11 12 13 NaN 15];

2. Find completely empty (all NaN) rows

Empty_rows = all(isnan(data), 2);

Disp(‘Empty Rows (All NaN):’);

Disp(find(empty_rows)); % Display row indices

3. Find completely empty (all NaN) columns

Empty_columns = all(isnan(data), 1);

Disp(‘Empty Columns (All NaN):’);

Disp(find(empty_columns)); % Display column indices


4.Count total number of empty rows and columns

Num_empty_rows = sum(empty_rows);

Num_empty_columns = sum(empty_columns);

Disp([‘Total Empty Rows: ‘, num2str(num_empty_rows)]);

Disp([‘Total Empty Columns: ‘, num2str(num_empty_columns)]);

Explanation:

1. All(isnan(data), 2) → Checks if all values in a row are NaN (empty row).


2. All(isnan(data), 1) → Checks if all values in a column are NaN (empty
column).
3. Find(empty_rows) and find(empty_columns) → Find row/column indices
of empty ones.
4. Counts total empty rows and columns for analysis.

MATLAB CODE FOR DATA UNIQUENESS

If you want to check data uniqueness in MATLAB (i.e., finding unique values,
duplicate values, or counting unique occurrences), here are different ways to
do it:

1. Find Unique Values in a Dataset

Sample dataset

Data = [1 2 3 4 5;

6 2 8 9 10;

1 12 3 14 15];
Find unique values

Unique_values = unique(data);

Disp(‘Unique Values in Data:’);

Disp(unique_values);

2. Count Unique Occurrences

Flatten data to a single column vector

Data_vector = data(;

Count occurrences of each unique value

[unique_vals, freq] = groupcounts(data_vector);

Disp(‘Unique Values and Their Frequencies:’);

Disp(table(unique_vals, freq));

3. Find Duplicate Values

Flatten data to a single vector

Data_vector = data(;

Find duplicate values

[~, idx, counts] = unique(data_vector, ‘stable’);

Duplicates = data_vector(histc(idx, 1:numel(data_vector)) > 1);

Disp(‘Duplicate Values in Data:’);

Disp(duplicates);

Explanation:

1. Unique(data) → Extracts unique values from thedataset.


2. Groupcounts(data_vector) → Counts how often each unique value
appears.
3. Finding duplicates:

MATALAB CODE FOR NaN FINDING

If you want to find NaN (missing values) in MATLAB, here’s a simple way to
do it:

MATLAB Code to Find NaN Values

1. Sample dataset with missing values (NaN)

Data = [1 2 NaN 4 5;

6 NaN 8 9 10;

11 12 13 NaN 15];

2. Find NaN values (Logical Matrix)

Nan_locations = isnan(data);

Disp(‘NaN Locations (Logical Matrix):’);

Disp(nan_locations);

3. Count total number of NaN values

Num_nans = sum(nan_locations();

Disp([‘Total Number of NaN Values: ‘, num2str(num_nans)])

4.Find row and column indices of NaN values

[row, col] = find(isnan(data));

Disp(‘NaN Locations (Row, Column):’);

Disp([row, col]);
Explanation:

1. Isnan(data) → Creates a logical matrix where 1 (true) indicates NaN


values.
2. Sum(nan_locations() → Counts total NaN values.
3. Find(isnan(data)) → Finds row and column indices of NaN values.

You might also like