如何根据条件替换某些列值?

发布于 2024-10-16 00:08:13 字数 1516 浏览 0 评论 0原文

我有一个矩阵 A

A=
     4.0000  120.0000   92.0000         0         0   37.6000    0.1910   30.0000
    10.0000  168.0000   74.0000         0         0   38.0000    0.5370   34.0000
    10.0000  139.0000   80.0000         0         0   27.1000    1.4410   57.0000
     1.0000  139.0000   60.0000   23.0000  846.0000   30.1000    0.3980   59.0000
     5.0000  136.0000   72.0000   19.0000  175.0000   25.8000    0.5870   51.0000
     7.0000  121.0000         0         0         0   30.0000    0.4840   32.0000

我想做两件事:

  1. 将第一列中大于 5 的值替换为 0。
  2. 在第二列中,如果值在 121- 范围内130,将其替换为 0。如果它们在 131-140 范围内,则将其替换为 1,将 141-150 替换为 2,将 151-160 替换为 3,依此类推。

因此,所需的结果矩阵将是:

A=
    4.0000   0.0000   92.0000         0         0   37.6000    0.1910   30.0000
    0.0000   4.0000   74.0000         0         0   38.0000    0.5370   34.0000
    0.0000   1.0000   80.0000         0         0   27.1000    1.4410   57.0000
    1.0000   1.0000   60.0000   23.0000  846.0000   30.1000    0.3980   59.0000
    5.0000   1.0000   72.0000   19.0000  175.0000   25.8000    0.5870   51.0000
    0.0000   0.0000         0         0         0   30.0000    0.4840   32.0000

我怎样才能完成此操作?

我正在尝试这样的事情:

counter=1;
for i = 1: rows
    if A(i,1) > 5
        A(i ,1) = 0;
    end
    if A(i,2) > 120 &&  A(i,2) < 130
        A(i ,2) = 0;
    end
    counter = counter+1;
end

使用案例可以解决问题吗?

I have a matrix A such that

A=
     4.0000  120.0000   92.0000         0         0   37.6000    0.1910   30.0000
    10.0000  168.0000   74.0000         0         0   38.0000    0.5370   34.0000
    10.0000  139.0000   80.0000         0         0   27.1000    1.4410   57.0000
     1.0000  139.0000   60.0000   23.0000  846.0000   30.1000    0.3980   59.0000
     5.0000  136.0000   72.0000   19.0000  175.0000   25.8000    0.5870   51.0000
     7.0000  121.0000         0         0         0   30.0000    0.4840   32.0000

I want to do two things:

  1. Replace the values of the first column that are greater than 5 by 0.
  2. In the second column, if the values are within the range 121-130, replace them by 0. If they are within the range 131-140, replace by 1, 141-150 by 2, 151-160 by 3, etc.

So the desired result matrix would be:

A=
    4.0000   0.0000   92.0000         0         0   37.6000    0.1910   30.0000
    0.0000   4.0000   74.0000         0         0   38.0000    0.5370   34.0000
    0.0000   1.0000   80.0000         0         0   27.1000    1.4410   57.0000
    1.0000   1.0000   60.0000   23.0000  846.0000   30.1000    0.3980   59.0000
    5.0000   1.0000   72.0000   19.0000  175.0000   25.8000    0.5870   51.0000
    0.0000   0.0000         0         0         0   30.0000    0.4840   32.0000

How can I accomplish this?

I was trying something like this:

counter=1;
for i = 1: rows
    if A(i,1) > 5
        A(i ,1) = 0;
    end
    if A(i,2) > 120 &&  A(i,2) < 130
        A(i ,2) = 0;
    end
    counter = counter+1;
end

Would using a case do the trick?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

长发绾君心 2024-10-23 00:08:13

您可以像这样修改 A 的前 2 列:

A(A(:,1) > 5,1) = 0;             %# Set values in column 1 greater than 5 to 0
A(:,2) = fix((A(:,2)-121)./10);  %# If the values in column 2 are all 120 or
                                 %#   greater you can shift, scale, then round
                                 %#   them towards 0 to get the new values

上面使用 矩阵索引向量化运算避免 for 循环或 case 语句。

You can modify the first 2 columns of A like so:

A(A(:,1) > 5,1) = 0;             %# Set values in column 1 greater than 5 to 0
A(:,2) = fix((A(:,2)-121)./10);  %# If the values in column 2 are all 120 or
                                 %#   greater you can shift, scale, then round
                                 %#   them towards 0 to get the new values

The above uses matrix indexing and vectorized operations to avoid for loops or case statements.

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