Index of first empty cell in array?
조회 수: 11 (최근 30일)
이전 댓글 표시
Hi, I would like to find the index of the first empty cell in an array consisting of n rows and 4 columns. I need the index, because I would then like to add a certain value to each of the non-empty rows in column 3. I tried the following, but got an error message "Index exceeds matrix dimensions.":
n=1;
while ~isempty(array{n,3})
array_new{n,3}=array{n,3}+deltax;
n=n+1;
end
My idea is to find out the index of the first empty cell to feed my while-loop with this index. Thank you!
댓글 수: 0
답변 (2개)
Giorgos Papakonstantinou
2014년 12월 18일
If you want to add a certain value to each of the none empty rows in column 3 maybe you try the following:
array(cellfun(@(x) ~isempty(x), array(:,1)), 3)
댓글 수: 0
Frank
2014년 12월 18일
편집: Frank
2014년 12월 18일
댓글 수: 1
Giorgos Papakonstantinou
2014년 12월 18일
편집: Giorgos Papakonstantinou
2014년 12월 18일
When the ~isempty(x) function is called, MATLAB assigns the value you pass in to variable x. In your case every cell entry of your array. (it does not correspond the value that you want to add).
A somewhat simplistic assumption of your array could be:
array =
[2] [3] [5] [3]
[] [1] [4] [1]
[4] [4] [5] [3]
[4] [4] [4] [1]
If you want i.e. to add 5 at each of the non empty rows of column 3, try this:
cellfun(@(x) x+5, array(cellfun(@(x) ~isempty(x), array(:,1)), 3), 'uni', 0).
This sentence will find the non-empty rows of the first column of array and it will add the value of 5 to the 3rd column. In the example that I gave you, it will be:
[7]
[7]
[6]
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!