# Find the indexes of all the zeros or nonzeros in a cell array that contains empty cells

조회 수: 42 (최근 30일)
Dennis Huver 2017년 9월 15일
댓글: Dennis Huver 2017년 9월 15일
Is there a way to find the indexes of all nonzero elements in a cell that contains empty cells too? I know of the way to first replace all of empty cells with 0 and they use the find and cellfun function. But is there a simpler way? Lets say A={2 5 0 10 0 [] 22 0 13} index=find(~cellfun(@isempty,A)) % index for non-empty cells Is there a way to find indexes for those who contain positive numbers only? Thanks btw using MatLab 2015a
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Stephen23 2017년 9월 15일
편집: Stephen23 2017년 9월 15일
Why are you storing numeric data in a cell array? Numeric arrays are the best kind of array for storing numeric data. Note that find is not required if you just want to replace empty arrays with zero:
A(cellfun('isempty',A)) = {0}

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

### 채택된 답변

Stephen23 2017년 9월 15일
편집: Stephen23 2017년 9월 15일
>> A = {2,5,0,10,0,[],22,0,13};
>> cellfun(@(a)~isempty(a)&&a>0,A)
ans =
1 1 0 1 0 0 1 0 1
But note that doing numeric operations on the contents of a cell array is quite inefficient. You could be much better off putting numeric data into a numeric array, and then all numeric operations will be much simpler and much more efficient:
>> A = {2,5,0,10,0,[],22,0,13};
>> A(cellfun('isempty',A)) = {NaN};
>> V = [A{:}]
V =
2 5 0 10 0 NaN 22 0 13
>> V>0
ans =
1 1 0 1 0 0 1 0 1
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Dennis Huver 2017년 9월 15일
it's working! thanks a ton!!

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

### 카테고리

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