erase a column that contains at least one zero

조회 수: 2 (최근 30일)
Sabbas
Sabbas 2012년 7월 5일
Hi all,
I have
[N,T,R]=xlsread(name);
R(:,2) contains NaNs and string variables. But sometimes R(:,2) contains and zeros. I want to check if R(:,2) contains at least one zero . If yes, then I want to erase the first column of N, that is N(:,1).
So I try this
R2 = R(:,2);
R22 = R2(cellfun(@(x)isnumeric(x),R2));
if any(cell2mat(R22)==0)
N(:,1) = [];
end
Is there any alternative which is more compact ?
  댓글 수: 4
Sabbas
Sabbas 2012년 7월 6일
thanks imager. I was looking for something maybe without the if statement. But if you agree with my approach and you find it correct then it is fine for me!
thanks again
Geoff
Geoff 2012년 7월 6일
Small syntax simplification....
cellfun(@(x)isnumeric(x),R2)
Can just be:
cellfun(@isnumeric,R2)

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 6일
N(:, any(cellfun(@(C) eq(C,0), R(:,2))) = [];
Two parts are involved here: one is detecting the zero in a column in one array, and the other is deleting column 1 in another array if the zero was found.
The three steps you use for detection can be merged together by various methods; I show one of them above.
In the above I also managed to merge the deletion into the single step, by using an obscure trick. It would not have worked if it had been anything other than column 1 to be removed, and the adjustment to handle some other arbitrary column would pile obscurity on obscurity. Using an "if" would be much more readable:
if any(cellfun(@(C) eq(C,0), R(:,2)); N(:,7) = []; end %for example

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by