deleting columns that contains 0,1 and negative numbers
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Minions
am 9 Feb. 2021
Kommentiert: Minions
am 12 Feb. 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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 9 Feb. 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 Kommentare
Walter Roberson
am 10 Feb. 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!