0

I would like to store the data from a double loop in a matrix (not a cell). I tried the following code (the function used in the inner loop here is just an example).

valuesforOPratio = zeros(41,1);
valuesforM = zeros(41,61);
NPVtotal=1;
for M = 40:100
for OPratio = 30:70;
NPVtotal = NPVtotal+1
valuesforOPratio(OPratio)=NPVtotal;
end
valuesforM(M) = valuesforOPratio
end
I get the following error:

 In an assignment  A(:) = B, the number of elements in A and B must be the same.

Error in sensitivity_opratio (line 10)
valuesforM(M) = valuesforOPratio

Any help on how to store the data in a matrix ? I guess this is rather easy but I am not getting there

2 Answers 2

2

There are a few problems with you code :

1/ You define valuesforOPratio as a 41x1 Vector. However, in the inner nested loop, the subscripts OPratio go from 30 to 70, meaning when you write valuesforOPratio(OPratio)=NPVtotal; the size of your valuesforOPratio vector will increase to 70.

To rectify that, you might want to either :

  1. Make your OPratio subscript go from 1 to 41 (i.e. for OPratio=1:41 ...)
  2. Set the right subscript in the call mentionned above (i.e. valuesforOPratio(OPratio-29)=NPVtotal;)

2/ When you write valuesforM(M)=valuesforOPratio, you are trying to put a vector (valuesforOPratio) in a scalar element (valuesforM(M)).

To rectify that, you just need to specify that you want a whole column of valuesforM to be filled with the values in valuesforOPratio, i.e. :

valuesforM(:,M-39)=valuesforOPratio;

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer it works perfectly fine. If i would change both loops to "parfor" loops would it still work the same way ?
0

Your valuesforM is a 41 by 61 matrix. If you want to write a row or colum you should wirte valuesforM(M:) = valuesforOPratio or valuesforM(:M) = valuesforOPratio

I havent MATLAB here on my computer to check my answer, but it should help you to find the solution.

1 Comment

thanks, now i get "subscripted assignment dimension mismatch .... " -any idea on that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.