0

On the first pass, everything is fine. i = 49, j = 1, k = 1112. On the second pass, i and j move to the next variables (50 and 18), but k stays at 1112. It does the same thing on the third and fourth pass. The result is that consolFut1 keeps getting overwritten. Can someone please point out my error? My sincerest gratitude for reading.

for i = 49:52
    expiry=find(vifDate==expDate(i));
    for j = [1; 18; 43; 63]
        vifCls1 = vifCls(j:expiry);
        for k = [1112; 1129; 1154; 1174]
            consolFut1 = consolFut(k:expDateIdx(i),i);
            arbVIF=vifCls1-consolFut1;
        end
    end
end
dlavila
  • 1,204
  • 11
  • 25
kits
  • 609
  • 6
  • 20
  • 1
    Use [the debugger](http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-177) to step through your code. – sco1 Jun 30 '15 at 14:33
  • 6
    Try row vectors i.e. `for k = [1112, 1129, 1154, 1174]` – Dan Jun 30 '15 at 14:33
  • @Dan is correct. By default when specifying a matrix as the variable to iterate over in a loop, it goes over each column, and so `k` would actually be a single vector of `[1112, 1129, 1154, 1174]` instead of the intended single value 1112. – rayryeng Jun 30 '15 at 14:58
  • I changed to row vectors but I still get the same result. Every loop still uses 1112. – kits Jun 30 '15 at 15:31
  • Maybe I'm misunderstanding your statements, but the second loop iteration should be `i=49, j=1, k=1129` all the way up to `i=49, j=1, k=1174`. Then it should continue the `j` loop with `i=49, j=18, k=1112`. So you'll execute the inner loop 64 times. Is that not what you're seeing? – beaker Jun 30 '15 at 16:27
  • Hi beaker, the second loop should be i = 50, j = 18, k = 1129. Does that make sense? – kits Jun 30 '15 at 17:24
  • @kits - You also need to change `j` to be a row vector as well. I missed that on the first read through. – rayryeng Jun 30 '15 at 17:30
  • @kits So you only want a total of 4 loops? – beaker Jun 30 '15 at 17:36
  • @beaker yes please 4 loops. Thank you so much for your help. – kits Jun 30 '15 at 17:40

1 Answers1

2

So it looks like what you really want to do is calculate the values 4 times, each time with the corresponding elements from your vectors i, j and k:

i = [  49   50   51   52]
j = [   1   18   43   63]
k = [1112 1129 1154 1174]

What you need to do is create a new variable that acts as an index into all three of these vectors. I'll call it x.

for x = 1:length(i)    %// == length(j) == length(k)
   %// add index (x) to all i, j and k
   expiry=find(vifDate==expDate(i(x)));
   vifCls1 = vifCls(j(x):expiry);
   consolFut1 = consolFut(k(x):expDateIdx(i(x)),i(x));
   %// Save results in arbVIF...
   %// use cell array since results will be of varying length
   arbVIF{x}=vifCls1-consolFut1;
end

I've made the assumption that arbVIF is the value you want to get out of all of this, and that you don't want it overwritten each time through the loop. So I've added the index (x) to it as well so that at the end of the loop arbVIF(1:4) will contain the results from each iteration of the loop. (Let me know if you need something different.)

Please note that it's generally not a good idea to use i and j as variables. See Using i and j as variables in Matlab.

Community
  • 1
  • 1
beaker
  • 16,331
  • 3
  • 32
  • 49
  • thanks beaker, everything is ok until the arbVIF line, where Matlab says " In an assignment A(I) = B, the number of elements in B and I must be the same." What did I do wrong? – kits Jun 30 '15 at 18:04
  • Okay, the results must have varying lengths. Try changing it to `arbVIF{x}` with curly brackets. That will change it to a cell array. – beaker Jun 30 '15 at 18:09
  • Thanks beaker, that works beautifully! I will avoid using i and j as variables in the future. – kits Jun 30 '15 at 18:16
  • @kits Good deal, glad I could help. – beaker Jun 30 '15 at 18:18