【问题标题】:Choosing cells in a struct with specific conditions在具有特定条件的结构中选择单元格
【发布时间】:2015-06-01 05:37:42
【问题描述】:

数据看起来更像这样:

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]},
           'Tr‌ial', 
           {'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]。有没有办法做到这一点?我想得到两件事:

  1. 包含ALL 2 OR ALL 1 并允许最多一个元素的单元格 那是不同的,所以我可以同时得到[2,2,2,2,2],[2,2,2,2,1,2] 不像我的代码只给出[2,2,2,2,2]
  2. 包含随机数的单元格

非常感谢您的帮助。

谢谢你

【问题讨论】:

    标签: arrays matlab struct


    【解决方案1】:

    您可以将条件更改为

    sum( x.Direction == 2 ) + 1 >= numel( x.Direction )
    

    这应该返回true,即使其中一个元素是1

    【讨论】:

    • 感谢您的回答。代码没有错误,但我仍然没有得到带有 [2,2,2,2,1,2] 的单元格。
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2011-01-14
    • 2015-08-22
    • 1970-01-01
    相关资源
    最近更新 更多