0

I am getting the error: "Subscript indices must either be real positive integers or logicals."

Is the fact that I have values as inputs and outputs that are decimals the issue?

Essentially, I call two matrices, M and N. Then I iterate through N, and depending on how N compares to something in M (less than, greater than, etc.), it puts the N value into an equation and outputs a value. Both the input from N and the output from the function are decimals.

I would like to fill priceB_values with the results of the for loop.

Is there something obvious that I'm missing?

 priceB_values = zeros(1:4)

for i = N(1:4,6)

if i < M(3,6)
    priceL_Opt1_Value = ((M(3,6)-M(3,5))/(M(1,6)-M(1,5)))*(priceB_Opt4-M(1,6))+M(3,6)   
elseif i > M(1,6) || priceB_Opt4 < M(1,7)
    priceL_Opt1_Value = ((M(3,7)-M(3,6))/(M(1,7)-M(1,6)))*(priceB_Opt4-M(1,7))+M(3,7)
elseif i > M(1,7) || priceB_Opt4 < M(1,8)
    priceL_Opt1_Value = ((M(3,8)-M(3,7))/(M(1,8)-M(1,7)))*(priceB_Opt4-M(1,8))+M(3,8) 
elseif i == M(1,5)
    priceL_Opt1_Value = M(1,5)
elseif i == M(1,6)
    priceL_Opt1_Value = M(1,6)
elseif i == M(1,7)
    priceL_Opt1_Value = M(1,7)
else i == M(1,8)
    priceL_Opt1_Value = M(3,8)    
end 

priceB_values(i) = priceL_Opt1_Value


end 
  • 2
    Read and think about the full error message (and provide it, so people can actually help you). It tells you exactly where the issue is. Though your provided code does not function (what is `N`?), I guarantee the issue is with `priceB_values(i) = priceL_Opt1_Value` since you're defining `i` as `N(1:4,6)` without ensuring this range is an array of integers. See http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol – sco1 May 01 '15 at 18:59
  • Sorry for the confusion-- I had seen that answer and it already did clarify that I have an issue with not integers--I know that. BUT what I don't know is how run a for loop and have it populate an array with decimal answers. I couldn't seem to find that answer, but I'm sure I'm missing something. Also M and N are matrices that are called (sorry that was hidden in the paragraph) – user2545406 May 01 '15 at 19:08
  • Yes I figured `M` and `N` are matrices, you need to provide them to us. I also don't understand what you're asking. Please provide a functioning example that replicates your issue along with a sample of the expected output. See [MVCE](http://stackoverflow.com/help/mcve) – sco1 May 01 '15 at 19:15

2 Answers2

0

Although it is hard to estimate where the problem is unless the variables are provided, several possible sources of error can be suggested:

  • Problem may be in the statement priceB_values = zeros(1:4), by calling zeros(1:4) you create the 4-dimentional array of zeros, so, if you want just to create a vector with 4 elements then you better change it to zeros(1,4).
  • Problem may lie in the i = N(1:4,6) statement. i is assigned with column vector that is compared with scalars in elseif statements, so you better change it to something like i = N(1:4,6)' that would transpose your
  • The non-integer numbers in N may also cause problem. Solution highly depends on what you need.
brainkz
  • 1,335
  • 10
  • 16
0

You probably want something like this:

priceB_values = zeros(1,6);
for p = 1:4,6
   ii = N(p);

   if ii < M(3,6)
      priceL_Opt1_Value = ((M(3,6)-M(3,5))/(M(1,6)-M(1,5)))*(priceB_Opt4-M(1,6))+M(3,6)   
   elseif ii > M(1,6) || priceB_Opt4 < M(1,7)
    priceL_Opt1_Value = ((M(3,7)-M(3,6))/(M(1,7)-M(1,6)))*(priceB_Opt4-M(1,7))+M(3,7)
   ...
   end 

   priceB_values(p) = priceL_Opt1_Value

end 

Now p is an integer index into both the list of inputs and the list of outputs, so priceB_values(1) through priceB_values(4) and priceB_values(6) will be valid and priceB_values(5) will be zero.

To get rid of priceB_values(5) (if you want that) you can do:

priceB_values = priceB_values(1:4,6);
beaker
  • 16,331
  • 3
  • 32
  • 49