Matlab 边界修剪
我有一个矩阵 A:
NaN NaN NaN NaN NaN NaN NaN 10 1 8 7 2 5 6 2 3 49 NaN NaN NaN NaN NaN NaN
我想知道是否有一种方法可以检测 NaN 何时第一次转向数字并转向第一个2 指向 NaN,例如 10 和 2 1 均为 NaN。
然后找出数字何时变为 NaN,并将最后两个数字点 3 和 49 变为 NaN。
最初我正在考虑使用以下方法,但我想知道这是否是最好的方法:
i= 2;
while i < 1440
if isnan(A(i)) < isnan(A(i-1)) //Transitioning from NaN to numbers
A(i:i+2) = NaN;
i = i+ 4;
elseif isnan(A(i)) > isnan(A(i-1)) //Transitioning from numbers to NaNs
A(i-2:i) = NaN;
i = i + 1;
else
i = i + 1;
end
end
但想知道是否还有其他方法可以优化它?
I have a matrix A:
NaN NaN NaN NaN NaN NaN NaN 10 1 8 7 2 5 6 2 3 49 NaN NaN NaN NaN NaN NaN
I was wondering if there was a way to detect when the NaNs first turn to numbers and turn the 1st 2 points to NaNs, such as the 10 & 1 both to NaN.
Then find when the numbers turn to NaNs and turn the last two number points, 3 and 49 to NaNs.
Originally I was thinking of using the following, but I was wondering if this was the best way:
i= 2;
while i < 1440
if isnan(A(i)) < isnan(A(i-1)) //Transitioning from NaN to numbers
A(i:i+2) = NaN;
i = i+ 4;
elseif isnan(A(i)) > isnan(A(i-1)) //Transitioning from numbers to NaNs
A(i-2:i) = NaN;
i = i + 1;
else
i = i + 1;
end
end
but was wondering if there was any other way I could optimize it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我假设你的向量 A 在开头和结尾处用 NaN 组织,中间有一组连续的数字,如
首先,我建议找到数字数据并从那里开始工作,如
现在 flagNumeric<对于数字条目,/em> 将为 true,对于 NaN 则为 false。
因此第一个数字将为 at
,最后一个数字为 at
然后您可以使用 firstIndex 和 lastIndex 将第一个和最后一个数字数据更改为 NaN
First I assume that your vector A is organized with NaN's at the start and end and a continous set of numerics in the middle, as in
First, I suggest locating the numeric data and working from there, as in,
Now flagNumeric will be a true for the entries that are numeric and false for NaN's.
So the first numeric will be at
and the last numeric at
You can then use firstIndex and lastIndex to change the first and last numeric data to NaN's
当然,上面的代码在特殊情况下会中断(例如,当
last == 1
时),但这些应该很容易过滤掉。Of course, the above code will break in special cases (e.g. when
last == 1
), but these should be straightforward to filter out.这是一个稍微简单的版本,基于与 Azim 的答案相同的假设:
Here's a slightly simpler version based on the same assumption as Azim's answer: