Matlab 边界修剪

发布于 2024-12-20 00:58:04 字数 619 浏览 2 评论 0原文

我有一个矩阵 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少跟Wǒ拽 2024-12-27 00:58:04

首先,我假设你的向量 A 在开头和结尾处用 NaN 组织,中间有一组连续的数字,如

A = [NaN ... NaN, contiguous numeric data, NaN ... NaN]

首先,我建议找到数字数据并从那里开始工作,如

flagNumeric = ~isnan(A);

现在 flagNumeric<对于数字条目,/em> 将为 true,对于 NaN 则为 false

因此第一个数字将为 at

firstIndex = find(flagNumeric,1,'first');

,最后一个数字为 at

 lastIndex = find(flagNumeric,1,'last');

然后您可以使用 firstIndexlastIndex 将第一个和最后一个数字数据更改为 NaN

 A(firstIndex:firstIndex+1) = NaN;
 A(lastIndex-1: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

A = [NaN ... NaN, contiguous numeric data, NaN ... NaN]

First, I suggest locating the numeric data and working from there, as in,

flagNumeric = ~isnan(A);

Now flagNumeric will be a true for the entries that are numeric and false for NaN's.

So the first numeric will be at

firstIndex = find(flagNumeric,1,'first');

and the last numeric at

 lastIndex = find(flagNumeric,1,'last');

You can then use firstIndex and lastIndex to change the first and last numeric data to NaN's

 A(firstIndex:firstIndex+1) = NaN;
 A(lastIndex-1:lastIndex) = NaN;
-柠檬树下少年和吉他 2024-12-27 00:58:04
% Set the first two non-NaN numbers to NaN
first = find(isfinite(A), 1, 'first');
A(first:first+1) = NaN;

% Set the last two non-NaN numbers to NaN
last = find(isfinite(A), 1, 'last');
A(last-1:last) = NaN;

当然,上面的代码在特殊情况下会中断(例如,当 last == 1 时),但这些应该很容易过滤掉。

% Set the first two non-NaN numbers to NaN
first = find(isfinite(A), 1, 'first');
A(first:first+1) = NaN;

% Set the last two non-NaN numbers to NaN
last = find(isfinite(A), 1, 'last');
A(last-1:last) = NaN;

Of course, the above code will break in special cases (e.g. when last == 1), but these should be straightforward to filter out.

枉心 2024-12-27 00:58:04

这是一个稍微简单的版本,基于与 Azim 的答案相同的假设:

nums = find(~isnan(A));
A( nums([1 2 end-1 end]) ) = NaN;

Here's a slightly simpler version based on the same assumption as Azim's answer:

nums = find(~isnan(A));
A( nums([1 2 end-1 end]) ) = NaN;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文