根据条件选择矩阵子集 [英] selecting subset of matrix based on condition

查看:68
本文介绍了根据条件选择矩阵子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下矩阵

test = [1 2 3 4;
        2 3 4 5;
        3 4 5 6;
        4 5 6 7;
        5 6 7 8];

我想选择,其第一个条目的值在1到3之间.我尝试过

I would like to select the rows whose first entry has a value between 1 and 3. I tried with

test(test(:,1)<3 && test(:,1)>1)

但是那给了我一个错误.然后我尝试了

but that gave me an error. Then I tried with

test(1<test(:,1)<3)

但这不能给我想要的结果2 3 4 5.有没有办法得到这个Matlab?

but that doesn't give me the desired result 2 3 4 5. Is there a way to obtain this is Matlab?

推荐答案

尝试一下,我无法在Matlab中对其进行测试,但应该可以.

Try this, I couldn't test it in Matlab but it should work.

test((1 < test(:,1) && test(:,1) < 3),:)

说明:

这 (1< test(:,1)&& test(:,1)< 3) 获取具有符合条件的行的二进制数组,然后使用它选择行.

This (1 < test(:,1) && test(:,1) < 3) Get's a binary array with the rows that fit the criteria, then you use that to select the rows.

有关更多信息,请参见此处.

See here for more information.

这篇关于根据条件选择矩阵子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆