Find a column number from a cell array
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a cell array from which I just know that some of its columns are "empty" and I would like to find those. For example I have an array:
C={1,'',6,11,''; 2,'',7,12,'';3,'',8,13,'';4,'',9,14,'';5,'',10,15,''}
And I would like my answers to be:
col_num1=2
col_num2=5
(meaning that 2nd and 5th columns are empty, the names for answer are not oblicatory but I would need them later on in my calculation)
댓글 수: 2
Guillaume
2016년 2월 9일
You do not want to create one variable per empty column. That is going to make subsequent code much harder to write.
You want it all in one variable so you can easily loop over the values.
As a rule, if you start numbering variables, you're doing it wrong. Whatever is in these numbered variables should be collected together in a matrix or cell aray.
채택된 답변
Guillaume
2016년 2월 9일
You need to apply the isempty function to each element of C to get a logical matrix. You can use an explicit loop or cellfun. It just then a matter of applying all to that logical matrix to find columns with all empty. Thus:
C = {1,'',6,11,''; 2,'',7,12,'';3,'',8,13,'';4,'',9,14,'';5,'',10,15,''};
emptycolumns = all(cellfun(@isempty, C))
%if you want the column number (often unnecessary, you can use the logical array directly)
emptycolumnsidx = find(emptycolumns)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!