A(I) = B, the number of elements in B and I must be the same.

Can someone please help me with this error message. I do not understand what is wrong with my code. Thank you again in advance.
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> DeclineCurveMCMC3 at 171 theta(i+1) = theta_ast;
I have attached the m file as was recommended, I apologize for the previous unreadable code format.

1 Comment

Please format your code to make it readable by following the tutorial at http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup.
Note, rather than pasting the block of code into your question body, you could have just attached the m file.

Answers (1)

The error is at line 171 of your code (as per the error message)
theta(i+1) = theta_ast;
You could debug this to see what is happening. Put a breakpoint at this line and re-run your function. When you get to the breakpoint, what is theta? what is theta_ast? You should notice that theta_ast is an empty matrix which was assigned as such in a previous line
theta_ast = proposal_PDF([]);
What is the code trying to accomplish in the above line? What do you want theta_ast to be? I suppose it should be a scalar of some kind (given the assignment to theta) but how do you intend to calculate this value?

4 Comments

the line of code
theta_ast = proposal_PDF([]);
is suppose to assign the value calculated in the previous calculation
proposal_PDF= proposal_PDF_q*proposal_PDF_Di*proposal_PDF_b;
to theta_ast
if a random number between 0 and 1 is less then the min(alpha,1), then theta(i+1) = theta_ast
does that clear your question up?
Okay but the code is using square brackets which indicates empty and when using empty as an index, nothing will be returned. For example, if
>> proposal_PDF = 0.4546
proposal_PDF =
0.4546
then
>> theta_ast = proposal_PDF([])
theta_ast =
[]
So the code is (kind of) saying take nothing from proposal_PDF and assign that nothing to theta_ast. And so theta_ast is just an empty matrix and when it is assigned to an element of theta the error arises. If proposal_PDF is just a single value, then do a straight assignment
>> theta_ast = proposal_PDF
or
>> theta_ast = proposal_PDF(1)
I made the two changes to my code below and that allowed the program to run, thank you for your help. I was stuck, now I can try to work on other aspects of it. thanks again!
theta_ast(i) = proposal_PDF;
%theta_ast = proposal_PDF([]); % Sampling from the proposal PDF
theta(i+1) = theta_ast(i);
Glad to have been of help!

This question is closed.

Asked:

on 27 May 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!