# How to remove all zeros in a matrix to use for calculations?

3 views (last 30 days)
bobby on 11 Oct 2021
Answered: Walter Roberson on 11 Oct 2021
Hello everyone!
I'm a bit stuck on how to remove all zeros from my matrix and using them to calculate basic statistical values such as the average. The zeros themselves represent blank data, therefore should be excluded from any form of calculations.
There isn't an exact pattern to where the zeros are located, so it makes it harder for me to work with it.
The following code is a simplified version of what I have thus far, however the values I get seem to be the exact same as if I did not include specific code targeting the zeros.
clear all
close all
clc
for i = 1:length(import)
Data = load(fullfile(import(i).folder, import(i).name));
%Meant to take all the zeros from the loaded matricies and turn them
%into Nan, then excluding them from calculations
Mean(i,:) = mean(Data.importData, 'all', 'omitnan');
Stdev(i,:) = std(Data.importData, 0,'all', 'omitnan');
Median(i,:) = median(Data.importData, 'all', 'omitnan');
%A custom function made to calculate mode
Mode(i,:) = halfrange(Data.importData, 'omitnan');
fileName(i,:) = convertCharsToStrings(strrep(import(i).name,'.mat','') )
end
%Create Table
tabData = table(fileName, Mean,Stdev,Median,Mode)
For reference, import is a workspace variable name that hold the matricies, which I am running through a loop. The import is being drawn from a seperate folder located outside of MATLAB.

Dave B on 11 Oct 2021
Edited: Dave B on 11 Oct 2021
I think your strategy looks good (although you're missing a ( in your example code)
Could it be that the values are not exactly zero? It's sometimes the case that values are very small and appear to be zero but are not quite zero, e.g. due to some floating point arithmetic error...
sum(Data.importData(:) == 0) % before setting them to NaN of course
If the result is 0, i.e. the 'zero' values are not exactly zero, maybe you want something more like:
eps = 1e-20; % choose an appropriate epsilon (a value that is conceptually 'zero' for your purposes)
Data.importData(abs(Data.importData) < eps) = nan;

### More Answers (1)

Walter Roberson on 11 Oct 2021
A replacement for
Data.importData(Data.importData==0) = nan;
would be
Data.importData = standardizeMissing(Data.importData, 0);
However, similar to @Dave B's concern, this would only match values that are exactly 0, not values that have small absolute value. For small absolute value, use the code that Dave B suggested.