在具有特定条件的结构中选择单元格
Choosing cells in a struct with specific conditions
数据看起来更像这样:
T = struct('Direction',
{[1,1,1,1],[1,1,2,1],[2,2,2,2,2],
[2,2,2,2,1,2], [2,2,2,2,2],[3,1,4,5]},
'Trial',
{'correct','incorrect','incorrect','correct','correct','incorrect'});
这只是一个例子,我还有其他领域
T = T(arrayfun(@(x) all(x.Direction == 2), T));
我已经有了上面的代码,它工作正常,但它只给了我 [2,2,2,2,2]
而没有给我带有 [2,2,2,2,1,2]
的单元格,因为它也有 1。
我尝试使用 <= 2
,但它也包括 [1,1,1,1],[1,1,2,1]
。有没有办法做到这一点?我想要 2 样东西:
- 单元格包含
ALL 2 OR ALL 1
并允许最多一个元素
那是不同的,所以我可以同时获得 [2,2,2,2,2],[2,2,2,2,1,2]
不像我的代码只给出 [2,2,2,2,2]
- 包含随机数的单元格
非常感谢您的帮助。
谢谢
您可以将条件更改为
sum( x.Direction == 2 ) + 1 >= numel( x.Direction )
这应该 return true
即使其中一个元素是 1
数据看起来更像这样:
T = struct('Direction',
{[1,1,1,1],[1,1,2,1],[2,2,2,2,2],
[2,2,2,2,1,2], [2,2,2,2,2],[3,1,4,5]},
'Trial',
{'correct','incorrect','incorrect','correct','correct','incorrect'});
这只是一个例子,我还有其他领域
T = T(arrayfun(@(x) all(x.Direction == 2), T));
我已经有了上面的代码,它工作正常,但它只给了我 [2,2,2,2,2]
而没有给我带有 [2,2,2,2,1,2]
的单元格,因为它也有 1。
我尝试使用 <= 2
,但它也包括 [1,1,1,1],[1,1,2,1]
。有没有办法做到这一点?我想要 2 样东西:
- 单元格包含
ALL 2 OR ALL 1
并允许最多一个元素 那是不同的,所以我可以同时获得[2,2,2,2,2],[2,2,2,2,1,2]
不像我的代码只给出[2,2,2,2,2]
- 包含随机数的单元格
非常感谢您的帮助。
谢谢
您可以将条件更改为
sum( x.Direction == 2 ) + 1 >= numel( x.Direction )
这应该 return true
即使其中一个元素是 1