deleting columns that contains 0,1 and negative numbers
조회 수: 7 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2021년 2월 9일
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
Walter Roberson
2021년 2월 10일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!