-1

My goal: Depending on which of the 5 scenarios is prevalent in each row, make the calculation following the if statement. End result should be 1 column, including the outcome of each row calc.

What I tried so far:

CalcOutcome = zeros(554,1);
  for k=height(MomPF)
        if MomPF.L_sum4t<0 & MomPF.U_sum4t>0           
            % make calc for every row but end result should only be 1 column
            % with the calc outcomes
            CalcOutcome=(-1)*MomPF.L_sum4t{k}*0.5 + MomPF.U_sum4t{k}*0.5;
        elseif MomPF.L_sum4t<0 & MomPF.U_sum4t<0
            CalcOutcome=(-1)*MomPF.L_sum4t{k}*1;
        elseif MomPF.L_sum4t>0 & MomPF.U_sum4t>0
            CalcOutcome=MomPF.U_sum4t{k}*1;
        elseif MomPF.L_sum4t>0 & MomPF.U_sum4t<0
            CalcOutcome=MomPF.L_sum4t{k}*0.5 + (-1)*MomPF.U_sum4t{k}*0.5;
        elseif MomPF.L_sum4t==0 & MomPF.U_sum4t==0
            CalcOutcome=0          
        end
   end

Table: enter image description here

John
  • 71
  • 7
  • What is your question? – sco1 Feb 15 '17 at 15:44
  • @excaza, see above (goal section) – John Feb 15 '17 at 15:48
  • 3
    A question usually ends with a question mark. (Please don't add "How do I achieve that goal?". [This](http://stackoverflow.com/help/how-to-ask) might help.) – dasdingonesin Feb 15 '17 at 15:52
  • Any reason your code doesnt work? Do you just need `CalcOutcome(k)=...`? – Ander Biguri Feb 15 '17 at 16:15
  • @AnderBiguri Exactly, the aim is to get CalcOutcome (554x1). So far CalcOutcome only returns zeros. If run separately, both the _if statements_ and _calculations_ seem to work; the problem is between linking those 2 and eventually storing the calculation outcome in the CalcOutcome variable. – John Feb 15 '17 at 16:29
  • It just seems that you are forgetting to index your data. Would all `if` condition have `MomPF.L_sum4t{k}`? You don't have the `{k}` anywhere – Ander Biguri Feb 15 '17 at 16:32

2 Answers2

1

I dont have your data to test, but I'd say you forgot to index!

CalcOutcome = zeros(554,1);
  for k=height(MomPF)
        if MomPF.L_sum4t(k)<0 & MomPF.U_sum4t(k)>0           
            % make calc for every row but end result should only be 1 column
            % with the calc outcomes
            CalcOutcome(k)=(-1)*MomPF.L_sum4t(k)*0.5 + MomPF.U_sum4t(k)*0.5;

        elseif MomPF.L_sum4t(k)<0 & MomPF.U_sum4t(k)<0
            CalcOutcome(k)=(-1)*MomPF.L_sum4t(k)*1;

        elseif MomPF.L_sum4t(k)>0 & MomPF.U_sum4t(k)>0
            CalcOutcome(k)=MomPF.U_sum4t(k)*1;

        elseif MomPF.L_sum4t>(k)0 & MomPF.U_sum4t(k)<0
            CalcOutcome(k)=MomPF.L_sum4t(k)*0.5 + (-1)*MomPF.U_sum4t(k)*0.5;

        elseif MomPF.L_sum4t(k)==0 & MomPF.U_sum4t(k)==0
            CalcOutcome(k)=0          
        end
   end

You are looping through k, use it.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • that's what I tried first, but the following errors occurred, so I dropped the `{k}`'s in the if statements `Cell contents reference from a non-cell array object. Error in matlab.internal.table.numArgumentsFromSubscriptRecurser (line 7) n = numArgumentsFromSubscript(a, s, ctxt); Error in tabular/numArgumentsFromSubscript (line 61) sz = matlab.internal.table.numArgumentsFromSubscriptRecurser(x,s(2:end),context);` – John Feb 15 '17 at 16:35
  • 1
    Based on the screen capture you provided, `MomPF.L_sum4t` is not a cell, so you can't index its contents with curly braces. You want regular braces: `MomPF.L_sum4t(k)`. – CKT Feb 15 '17 at 16:37
  • 1
    @John ......... That is a completelly different thing that what you asked. And its because `{k}`->`(k)`. Try the current edit. For future reference, take your time to read [ask], and very importantly, [mcve] – Ander Biguri Feb 15 '17 at 16:37
  • @AnderBiguri still returns zeros but CKT's approach seems to work perfectly. But thanks for your time and effort – John Feb 15 '17 at 16:45
  • @John if it's still returning zeros then it's likely your logic is wrong, and in the process of rewriting it to CKT's approach you fixed the logic statements. In the future I would recommend using the debugger to make sure your conditions are actually formulated correctly. – sco1 Feb 15 '17 at 17:03
  • @excaza that's noted on my side. Thanks for this! – John Feb 15 '17 at 17:05
1

As others have mentioned, it appears the indexing is the problem. That said, you don't need to loop -- you should be able to do all of this at once with table indexing. For example, something like:

idx = (MomPF.L_sum4t < 0) & (MomPF.U_sum4t > 0); CalcOutcome(idx) = -0.5*MomPF.L_sum4t(idx) + 0.5*MomPF.U_sum4t(idx);

And then rinse and repeat for the other conditions.

CKT
  • 781
  • 5
  • 6
  • thanks a lot! Exactly doing what I intended to do after having repeated for the other cases. – John Feb 15 '17 at 16:48