Delete Vector Element but Keep Vector Length

2 Ansichten (letzte 30 Tage)
Grant Kitchen
Grant Kitchen am 28 Jan. 2022
Kommentiert: Walter Roberson am 29 Jan. 2022
Say we have two vectors, one for time and one for position.
X = [0,1,2,3,4,5,6,7,8,9,10]
Y =[0,2,4,6,8,143,12,14,16,18,20]
We want to delete 23 from Y, but keep the vector the same length, and not cause errors. We want the X Y matrix to just have an empty value. If we delete the 6th element in Y, all of a suddent Y is 10 elements instead of 11. If we replace with NaN, we get NaN when we want no number. If we replace with 0 we get 0 instead of empty.
Ultimately, we want to plot X and Y to show a trend, and we know the 6th element is an outlier that exceeds the measurement range. But how do we flag it? Is the only option to also delete the 6th element of X to match them up?
  1 Kommentar
Torsten
Torsten am 28 Jan. 2022
Of course. And it's most logical because you have no y-value at x=5.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Steven Lord
Steven Lord am 28 Jan. 2022
X = [0,1,2,3,4,5,6,7,8,9,10];
Y =[0,2,4,6,8,143,12,14,16,18,20];
[newY, removedOutliers] = rmoutliers(Y);
newX = X;
newX(removedOutliers) = []; % Remove corresponding elements from X as well
plot(newX, newY)
Alternately if you want to show that something was removed, filling that value in Y with a NaN may be the most appropriate solution. You really didn't have data there, so why show a line across there that may mislead viewers into thinking you did?
newY2 = filloutliers(Y, NaN);
plot(X, newY2, 'o-')
  2 Kommentare
Grant Kitchen
Grant Kitchen am 28 Jan. 2022
So if we fill with NaN, and we do a further calculation with the vector, say of slope, will having the NaN in there mess it up or will MATLAB just ignore the NaN?
Walter Roberson
Walter Roberson am 29 Jan. 2022
Any element-wise arithmetic calculation involving NaN results in NaN
Matrix calculations such as matrix-multiply or matrix divide, that involve a NaN, will typically result in even more NaN.
Some functions such as sum() have specific options to ignore NaN (or not)

Melden Sie sich an, um zu kommentieren.


Voss
Voss am 28 Jan. 2022
Note that using NaN's in a vector you're plotting is useful to show that something is missing:
X = [0,1,2,3,4,5,6,7,8,9,10];
Y =[0,2,4,6,8,23,12,14,16,18,20];
figure();
plot(X,Y,'-o','LineWidth',2);
Y(Y > 20) = NaN;
hold on
plot(X,Y,'--rx','LineWidth',2);
As opposed to deleting the outliers from the vectors:
X = [0,1,2,3,4,5,6,7,8,9,10];
Y =[0,2,4,6,8,23,12,14,16,18,20];
figure();
plot(X,Y,'-o','LineWidth',2);
idx = Y > 20;
X(idx) = [];
Y(idx) = [];
hold on
plot(X,Y,'--rx','LineWidth',2);

Walter Roberson
Walter Roberson am 28 Jan. 2022
X = [0,1,2,3,4,5,6,7,8,9,10]
X = 1×11
0 1 2 3 4 5 6 7 8 9 10
Y =[0,2,4,6,8,143,12,14,16,18,20]
Y = 1×11
0 2 4 6 8 143 12 14 16 18 20
Y(6) = NaN
Y = 1×11
0 2 4 6 8 NaN 12 14 16 18 20
plot(X, Y)
Since your purpose is plotting, if you just drop in NaN then no connection will be drawn.
But you might prefer
X = [0,1,2,3,4,5,6,7,8,9,10]
X = 1×11
0 1 2 3 4 5 6 7 8 9 10
Y =[0,2,4,6,8,143,12,14,16,18,20]
Y = 1×11
0 2 4 6 8 143 12 14 16 18 20
where_out = find(isoutlier(Y, 'SamplePoints', X));
newY = filloutliers(Y, 'spline', 'SamplePoints', X);
plot(X, newY, '-*', 'Color', 'b', 'MarkerEdgeColor', 'r', 'MarkerIndices', where_out)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by