deleting columns that contains 0,1 and negative numbers

8 vues (au cours des 30 derniers jours)
Minions
Minions le 9 Fév 2021
Commenté : Minions le 12 Fév 2021
I have a large file, i want to delete the colums that contain 0,1 or negative values. I am new in matlab, any help will be appreciated. thank you. Until now. i tried to write the following lines to execute the file but it is not working
f1=readtable('test1.csv'); %Read file 1
d1=table2array(f1);
d1(:,1)=[] % removing the first column as it is empty
d1(d1(:,1)<0,:)=[] % removing the columns that are less than 0

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Fév 2021
d1 = readmatrix('test1.csv');
d1 = d1(:,2:end); %removing the first column as it is empty
mask = all(d1 <= 0 || d1 == 1,1);
d1(:,mask) = []; %remove columns that contain only 0, 1, or negative values
  5 commentaires
Walter Roberson
Walter Roberson le 10 Fév 2021
mask is not a function, it is a variable here. It is being assigned the result of running all() over the first dimension of the result of the expression inside the all() call. The expression tests each entry in d1 for non-positive, and tests each entry for being exactly 1, and "or" the two results together. The result of the expression will be true for each location in d1 that is non-positive or is exactly 0, and false for each location that is positive but not exactly 0.
When you use all() over the first dimension of that array of logical values, you will get true for every column in which every value is either non-positive or exactly 1, and you will get false if there is even a single entry in the column that is positive but not exactly 1.
Once you have that vector of values, it is used for "logical indexing": any column with a true value is to be selected for deletion in the = [] statement.
Question: do you have any values that are positive greater than 0 but less than 1 and which you wish to leave alone? If so then the current code is needed; but if you do not have any such values, then you could potentially simplify the test to be d1 <= 1
Minions
Minions le 12 Fév 2021
I understand, thank you fro explaining it briefly.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by