Remove negative digits from a cell array
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi all.
If I have a number like that 1 1 4 1 5 6 4 2 -2 3 -2 3 6 1 1 -2 -2 -2 -2 9.
If I don't want the negative digits, how can I do it?
Thank you in advance for any possible answers.
Pedro
댓글 수: 0
답변 (1개)
dpb
2023년 4월 17일
이동: dpb
2023년 4월 17일
There's no cell array in sight in the above and how do you want to eliminate them -- replace with something else or remove entirely so the array is smaller?
x{1}=[1 1 4 1 5 6 4 2 -2 3 -2 3 6 1 1 -2 -2 -2 -2 9]
Remove
x{:}
x{1}>0
x{1}(x{1}>0)
x={x{1}(x{1}>0)}
You first have to dereference the cell array to get to the content, then do the logical addressing on that array -- then put that back into the cell.
If it is a cell array, then @cellfun will be helpful
댓글 수: 3
Image Analyst
2023년 4월 17일
편집: Image Analyst
2023년 4월 17일
Is that a cell array? Please see https://matlab.fandom.com/wiki/FAQ#What_is_a_cell_array?
Or a double matrix? If it's a matrix, you can't "remove" elements because it must remain rectangular. You can't have matrices with "ragged" right or bottom edges because rows or columns were shifted, and you can't have "holes" in the matrix, though you can assign NaN to those locations if you want.
dpb
2023년 4월 17일
편집: dpb
2023년 4월 17일
I had @Image Analyst's response in preparation but a meeting pulled me away before posting...specifically what I was going to illustrate is something that is the best approximation one can make other than insertion of the missing value (NaN for double) is to create a cell array
N=8; % dataset small enough to observe
x=randi([-2 9],N) % create a set of data
x=num2cell(x,2) % turn into set of row cell arrays
cellfun(@(c)c(c>=0),x,'uniformoutput',0) % keep only positive values each cell
Now you have only the positive values left, but it isn't in a regular 2D array any more (and can't be), but it meets the request to remove the negative values.
Depending upon what it is that is/are the next step(s), it may be simpler to just treat the missing values instead; functions like mean, etc., all have flags to 'omitnan'.
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!