Replacing Negative Values in Table with Previous Value in Column
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Stef1998
am 22 Nov. 2021
Beantwortet: Peter Perkins
am 23 Nov. 2021
I have a table 17520x5, in the last 2 columns I would like to replace all negative values with the previous value in that column. This is what I have tried so far and it is not working I still get negative values shown.
My table (T) had 5 columns, variable labels are (A, B, C, D, E) for each column for example
D(D < 0) = NaN;
E(E < 0) = NaN;
T(:, {'D', 'E'}) = fillmissing(T(:,{'D', 'E'}), 'previous');
disp(T)
0 Kommentare
Akzeptierte Antwort
Matt J
am 22 Nov. 2021
Bearbeitet: Matt J
am 22 Nov. 2021
T(:, {'D', 'E'}) = num2cell( fillmissing([D,E], 'previous') );
Weitere Antworten (1)
Peter Perkins
am 23 Nov. 2021
Stef, as near as I can tell, the only thing wrong with your original solution is that D and E are in T, not in the workspace. The following works fine, including repeated negative values and negative values in the first row. There's no need to explicitly pull D and E out of the table. Using Matt's setup:
>> T = array2table(rand(4,5)-0.5 ,'Var',["A" "B" "C" "D" "E"])
T =
4×5 table
A B C D E
________ ________ _________ ________ _________
-0.33782 -0.33435 0.18921 -0.27102 0.038342
0.29428 0.10198 0.24815 0.41334 0.49613
-0.18878 -0.23703 -0.049458 -0.34762 -0.42182
0.028533 0.15408 -0.41618 0.32582 -0.057322
>> T.D(T.D < 0) = NaN;
>> T.E(T.E < 0) = NaN
T =
4×5 table
A B C D E
________ ________ _________ _______ ________
-0.33782 -0.33435 0.18921 NaN 0.038342
0.29428 0.10198 0.24815 0.41334 0.49613
-0.18878 -0.23703 -0.049458 NaN NaN
0.028533 0.15408 -0.41618 0.32582 NaN
>> T(:, ["D" "E"]) = fillmissing(T(:,["D" "E"]), 'previous')
T =
4×5 table
A B C D E
________ ________ _________ _______ ________
-0.33782 -0.33435 0.18921 NaN 0.038342
0.29428 0.10198 0.24815 0.41334 0.49613
-0.18878 -0.23703 -0.049458 0.41334 0.49613
0.028533 0.15408 -0.41618 0.32582 0.49613
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical 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!