Delete Vector Element but Keep Vector Length

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 个评论

Of course. And it's most logical because you have no y-value at x=5.

请先登录,再进行评论。

回答(3 个)

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 个评论

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?
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)

请先登录,再进行评论。

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);
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)

类别

帮助中心File Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by