仅选择满足条件的特定行数

发布于 2024-12-15 08:56:59 字数 604 浏览 4 评论 0原文

我目前开始使用八度进行一些数据分析,并且在特定的矩阵操作方面遇到一些问题。

假设您有以下数据矩阵:

    A =

        1   11   22   33
       44   13   12   33
        1   14   33   44

现在我想删除该矩阵中未满足以下条件的所有行。

    octave:6> A(:, 4) == 33
    ans =

       1
       1
       0

我将得到这种形式的矩阵,它只选择这些行:

    A_new =

        1   11   22   33
       44   13   12   33

我知道在一些循环的帮助下这是可能的。但是是否有一个更干净的解决方案,例如通过使用提供的标准库?那太好了:]

R 也已经发布了一些类似的问题: 在 R 中,选择矩阵中满足条件的行一个条件

I currently started to work with octave for some data analysis and have some problems for a specific matrix manipulation.

Assume you have the following data matrix:


    A =

        1   11   22   33
       44   13   12   33
        1   14   33   44

Now I would like to delete all rows of this matrix which don't accomplish e.g. the following condition.


    octave:6> A(:, 4) == 33
    ans =

       1
       1
       0

And I'll get the matrix of this form which only selects these rows:


    A_new =

        1   11   22   33
       44   13   12   33

I know this is possible with the help of some loops. But is there maybe a cleaner solution e.g. by using the provided standard library? That would be great :]

Some similar question was also already posted for R:
In R, select rows of a matrix that meet a condition

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

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

发布评论

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

评论(1

感受沵的脚步 2024-12-22 08:56:59

尝试:

A = [
    1   11   22   33
    44  13   12   33
    1   14   33   44
];
idx = ( A(:,4)==33 );
A_new = A(idx,:)

这是使用逻辑索引

Try:

A = [
    1   11   22   33
    44  13   12   33
    1   14   33   44
];
idx = ( A(:,4)==33 );
A_new = A(idx,:)

This is using logical indexing

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