Can I assign a single value to multiple elements of a cell array without a loop
조회 수: 14 (최근 30일)
이전 댓글 표시
For example, I have an array that looks like
myArray =
[3] [] [3] []
I'd like to fill out the empty elements of myArray with zeros, but, obviously, the following doesn't work:
myArray{find(cellfun(@isempty,myArray))} = 0
Obviously, I could write a loop, but there has to be a better way!
Thanks for any suggestions!
댓글 수: 0
채택된 답변
James Tursa
2015년 2월 17일
편집: James Tursa
2015년 2월 17일
You almost had the syntax right:
myArray(find(cellfun(@isempty,myArray))) = {0}
Or you can just use logical indexing and skip the find:
myArray(cellfun(@isempty,myArray)) = {0}
댓글 수: 1
Stephen23
2015년 2월 18일
편집: Stephen23
2015년 2월 18일
The find is completely superfluous and just slows the code down, so use the second version (uses logical indexing ). A slightly faster alternative is to use the overloaded isempty option:
myArray(cellfun('isempty',myArray)) = {0}
추가 답변 (1개)
Guillaume
2015년 2월 17일
Your main problem is the confusion between () and {}. Secondly, use logical indexing, rather than find:
myArray(cellfun(@isempty, myArray)) = {0};
댓글 수: 0
참고 항목
카테고리
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!