【问题标题】:Conditional selection in sublist using Select in Mathematica使用 Mathematica 中的 Select 在子列表中进行条件选择
【发布时间】:2011-06-28 16:46:56
【问题描述】:

考虑以下列表:

answers = {
          {{1, 2}, {7, 3}}, {{1, 3}, {6, 4}}, 
          {{2, 1}, {2, 8}}, {{2,3}, {8, 2}}, 
          {{3, 1}, {1, 9}}, {{3, 2}, {3, 7}}
          }

在任务的上下文中,受试者依次呈现 2 个刺激,必须选择他们喜欢的一个,第一个子列表:

{{1, 2}, {7, 3}}

可以读作

{{Cond1,Cond2}, {Cond1 首选计数,Cond2 首选计数}}

因此,当首先呈现 Cond1 时,在 Cond1 和 Cond2 之间选择 10 次时,Cond1 是 10 次中的 7 次。

**

我需要有条件地提取和/或 对列表的一部分求和。

**

到目前为止我一直在做什么:

提取首先出现 Cond1 的列表:

Select[answers, #[[1, 1]] == 1 &]

= {{{1, 2}, {7, 3}}, {{1, 3}, {6, 4}}}

并获得一个条件对所有其他条件的总计数:

Plus @@ Select[answers, #[[1, 1]] == 1 &][[All, 2]]

={13, 7}

现在,我需要:

查询 Cond1 VS Cond2 & Cond2 VS Cond1 的总数:

{{{1, 2}, {7, 3}},{{2, 1}, {2, 8}}}

将是输出

或 Cond2 首先针对 Cond3 呈现时的总计数:

{{2,3}, {8, 2}}

这些是我所缺少的。 现实中一共有5个条件

【问题讨论】:

    标签: select wolfram-mathematica conditional


    【解决方案1】:

    我喜欢Cases,因为我认为语法更清晰。这是一种方法,使用了几个轻量级的实用函数:

    In[25]:= conditions[pat_] := Cases[answers, {pat, _}]
    
    In[26]:= conditionSums[pat_] := Total[Last /@ conditions[pat]]
    
    In[27]:= conditions[{1, _}]
    
    Out[27]= {{{1, 2}, {7, 3}}, {{1, 3}, {6, 4}}}
    
    In[28]:= conditionSums[{1, _}]
    
    Out[28]= {13, 7}
    
    In[29]:= conditions[{1, 2} | {2, 1}]
    
    Out[29]= {{{1, 2}, {7, 3}}, {{2, 1}, {2, 8}}}
    
    In[30]:= conditions[{2, 3}]
    
    Out[30]= {{{2, 3}, {8, 2}}}
    

    【讨论】:

    • Brett,非常感谢你,我非常喜欢这个,如此清晰。我将第一次使用模式!
    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多