Remove negative digits from a cell array
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
dpb
am 17 Apr. 2023
Verschoben: dpb
am 17 Apr. 2023
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 Kommentare
Image Analyst
am 17 Apr. 2023
Bearbeitet: Image Analyst
am 17 Apr. 2023
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
am 17 Apr. 2023
Bearbeitet: dpb
am 17 Apr. 2023
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'.
Siehe auch
Kategorien
Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!