How can I delete Columns containing Zeros without deleting small decimals in a table?

3 views (last 30 days)
I have a large table containing small numbers. Some columns have zeros and I would like to delete those columns without deleting the other columns containing very small decimal points (ex. 0.000002). I can not directly call out the column and delete it because depending on which table I load, the columns containing 0 might be in a different location.Here is an example of the data:
I am relatively new to coding would appreciate detailed suggestions.
  2 Comments
Walter Roberson
Walter Roberson on 13 Oct 2023
Do you want to delete a column if it contains even a single exact zero, or do you only want to delete columns that are entirely zero?
Is your table entirely numeric, or does the code need to figure out which columns are numeric?
Which MATLAB release are you using?
Maya Gueissaz-Daoust
Maya Gueissaz-Daoust on 13 Oct 2023
I want to delete columns that are entirely exactly 0, and the table is entirely numeric. I am using Matlab R2021b

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 13 Oct 2023
Edited: Dyuman Joshi on 13 Oct 2023
Assuming, the task is to delete columns where all entries are 0.
This might be one of rare cases where comparing floating point numbers with eq, == or isequal works -
%Let A be the name of your table
idx = varfun(@(x) all(x==0), A,"OutputFormat","uniform");
A(:,idx) = [];
Edit - If all the data in table is numeric, why not read/store it as a numeric array?
  1 Comment
Walter Roberson
Walter Roberson on 13 Oct 2023
A logically equivalent version:
idx = varfun(@any, A, "OutputFormat","uniform");
A(:,~idx) = [];
However, both your code and this code would be challenged if one of the table variables has multiple columns.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!