2014-09-29 59 views
2

我很抱歉问这个家伙,但这个问题一直是我一个星期存在的祸根。我现在应该已经能够弄清楚了,但我似乎无法让它起作用。我需要做的是编码进行个性测试。它基于这个圈子:个性测试MATLAB

      1 
         10  2 

        9    3 

        8    4 

         7   5 
          6   

所以我已经有我的输入给我了。我有三个:type1,pVec1和subType1。 pVec是我假设我必须索引的一个向量。 type1给我的是我的显性或隐性特征。我所要做的就是确定我的其他特征。如果他们给我我的主要特征,那么第一个字母将会被大写。如果它是隐性的,则是小写字母。显性和隐性特质总是相互对立的。如果我有3型显性人格,我的隐性将是8型(使用圆圈)。此外,我的翅膀个性类型是基于我的主导类型。它们是左边和右边的数字(在这种情况下,2和4是我的翼型人格类型)。子类型只是告诉我是否添加关于我的主导类型的内容。

Test Cases: 
     type1 = 'helper'; 
     pVec1 = ['Reformer Helper Achiever Individualist Dreamer Investigator  
       Loyalist Enthusiast Challenger Peacemaker']; 
    subType1 = 'alone'; 
[dom1, rec1, wings1] = personalityTest(type1,pVec1,subType1); 
     dom1 > 'Loyalist_a' 
     rec1 => 'Helper' 
     wings1 => 'Enthusiast Investigator' 

所以我必须让它做这样的事情。到目前为止,我有:

function[dominant, recessive, wings] = personalityTest(type, pVec, subtype) 
type_list = strsplit(pVec); 

position = find(strcmpi(type, type_list), 1, 'first'); 
dominant = upper(type(1)); 
dominant = char(type); 
recessive = lower(type(1)); 
recessive = char(type); 
switch type 
case recessive 
    dominant = position + 5; 
case dominant 
    recessive = char(dominant - 5); 
end 

leftwing = char(dominant + 1); 
rightwing = char(dominant - 1); 

wings = [leftwing rightwing]; 
if strcmp(subtype, 'alone') 
    dominant = [dominant '_a']; 
elseif strcmp(subtype, 'smallGroup') 
    dominant = [dominant, '_s']; 
else 
    dominant = [dominant, '_1']; 
end 

我的功能不断给我一个空白的显性(或只是_A)。我如何解决这个问题?我想让它在第五位给我这个词。

+0

既然神经科学用“性格测试”的东西来显示它的颜色! – Divakar 2014-09-29 16:40:31

+0

那个男人,那是心理学 – 2014-09-29 16:43:03

+0

那些与大脑/头脑事物有关的东西不是那些相关的东西吗? :) – Divakar 2014-09-29 16:45:26

回答

3

单步执行代码将有助于向您揭示错误。

type = 'helper'; 
pVec = ['Reformer Helper Achiever Individualist Dreamer Investigator  
      Loyalist Enthusiast Challenger Peacemaker']; 
subType = 'alone'; 

执行

type_list = strsplit(pVec); 

position = find(strcmpi(type, type_list), 1, 'first'); 
dominant = upper(type(1)); 
dominant = char(type); 
recessive = lower(type(1)); 
recessive = char(type); 

结果dominantrecessive被设置为这些值

dominant = 'helper' 
recessive = 'helper' 

这里的目的,我认为你的函数的后续行,就是要确定类型字符串的第一个字符是大写或小写。换句话说,我们需要TrueFalse的值为dominantrecessive

函数upper()将输入stringchar转换为大写。要检查第一个字符是大写或小写,尽量

dominant = type(1) == upper(type(1)) 
recessive = type(1) == lower(type(1)) 

的分配

dominant = char(type); 
recessive = char(type); 

是不必要的(它们将覆盖以前的计算!)。

现在下一行代码将更接近于产生正确的输出。

switch type 
case recessive 
    dominant = position + 5; 
case dominant 
    recessive = char(dominant - 5); 
end 

这里有三个(也许是四个)问题。

  1. dominant = position + 5。如果position的值为9,则dominant将等于14。由于这里的意图是用dominant索引到类型列表中,所以可能会导致数组索引超出范围错误。解决这个问题的方法是使用模数(余数)函数,即dominant = (position + 4) % 10 + 1。其余功能基本上是“什么是(p + 5)通过10分割后的余数。怪异1和4是保留可能分配给dominant是在[1,10]的值的范围。

  2. 如果个性特征向量(pVec)始终为长度10,然后将5硬编码到计算中是可以的,但是如果pVec的长度可以变化,那么您需要确定正确的值以将位置增加。如果长度为那么应该是length(pVec)/2

  3. 该行recessive = char(dominant - 5)将评估为recessive = char('helper' - 5)在您当前的代码迭代中。这并没有什么意义,因为您正在从字符串中减去一个整数,然后再次转换为字符串。相反,计算类型列表中隐性特征的索引。这与2中列出的过程完全相同。

  4. 使用表示它们所做的变量名将帮助您更清楚地了解您的代码。

  5. 目标是使用dominantrecessive作为type_list的索引。您忘记在每个开关盒中设置2个索引中的1个。代码

下一页线:

leftwing = char(dominant + 1); 
rightwing = char(dominant - 1); 

指数为type_list获得的左右两翼正确的价值观。小心数组索引超出界限的错误。

下一页线:

wings = [leftwing rightwing]; 
if strcmp(subtype, 'alone') 
    dominant = [dominant '_a']; 
elseif strcmp(subtype, 'smallGroup') 
    dominant = [dominant, '_s']; 
else 
    dominant = [dominant, '_1']; 
end 

再次,索引问题。解决这些问题,你可以免费回家。