0

Let's say I have a data table that is 1x15

 [21 78 33 59 90 26 88 54 36 63 72 37 48 93 56]

So for my streaking equation I need to implement this:

% Streaking = {[abs(Q_n - ((Q_n-1 + Q_n+1)/2))] / ((Q_n-1 + Q_n+1)/2)))}*100

Explanation of the Equation:

Let's assign the value 90 to Q_n (this comes from cell (1,5)) Using this value would mean that for Q_n-1 we'd want the value 59 and for Q_n+1 we'd want the value 26 (so in essence the cell before (1,4) and the cell after (1,6))

Q_n = 90

Q_n-1 = 59

Q_n+1 = 26

My question is: How would I do this for the entire data set and apply them to the streaking equation? (excluding row 1 and 15 as you cant have a Q_n-1 for row 1 and you cant have a Q_n+1 for row 15)

I was thinking of something maybe along the lines of this:

Q_n = 1;
while Q_n < length(middle.middle_interpolate)
    Q_n = Q_n+1;
    before = middle.middle_interpolate{Q_n-1};
    actual = middle.middle_interpolate{Q_n};
    after = middle.middle_interpolate{Q_n+1};
    averg = ((before + after)/2);
    equation = (abs(actual-averg)/averg)*100;
    plot(equation);
end
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
Ben B.
  • 97
  • 3
  • 11
  • Why are you using cells instead of matrices? If you input a vector you can do everything without a loop. The main problem seems to be that in the last iteration 'Q_n==length(input)' and you access 'Q_n+1'. Just change you're boundary to ' – denahiro Jul 17 '12 at 14:49

1 Answers1

1

I broke this out into parts so you could see what I was doing. It also only works for this particular dataset since the indicies are hardcoded (but I bet you can figure out how to make it work for any dataset you have).

averg = mean([Qn(1:13),Qn(3:15)],2); % this is just getting your averg value.
actual = Qn(2:14);
equation = abs(actual-averg)./averg*100;
plot(equation)

Of course you could put this all on one line and make it look crazy complicated...

equation = abs(mean([Qn(1:13),Qn(3:15)],2) - Qn(2:14))./mean([Qn(1:13),Qn(3:15)],2)*100

But you want to keep it somewhat readable for others who might look at the code in the future.

Ben A.
  • 1,039
  • 6
  • 13
  • so the 13 is just 2 under the max count, so i could do something like length(data) - 2 instead for my actual data? as well as replacing 15 with just length(data) and then 14 with length(data) -1 ? – Ben B. Jul 17 '12 at 15:55
  • That would be correct, and if it's just a 1xn vector you can use `end` instead of `length(data)` – Ben A. Jul 17 '12 at 15:57
  • Okay, So I had another answer submitted on the matlab forums and it appears that both of these are doing the same things, the only discrepancy is that Your answer starts the result at row 1, while the other applies a NaN (since how i explained why it couldnt be used refering to the first and last row). Regardless Thank you so much for such a quick and clear response. – Ben B. Jul 17 '12 at 16:25
  • No problem at all. If you want to try to take into account the data in the first and last position you can always pad the matrix with some value. You can find some information about that through Matlab's help on the `padarray` function or just using that logic and making your own pad since you really only need to pad the top and bottom. – Ben A. Jul 17 '12 at 16:31
  • It's actually nice that yours didnt include the NaN's since I'm looking for the largest value and i can just quickly glance at the max column in the workspace for the variable and get that for what I'm doing. – Ben B. Jul 17 '12 at 16:36