How do I make a matrix in a specific for loop?

1 view (last 30 days)
I am using a for loop with the function "fminsearch" and want to save the data into a matrix, with each iteration of the for loop saving to the subsequent cell, rather than replacing the variable name with each iteration.
Here is the code that I have:
options = optimset();
for i=1:7
best_fit_eff(i) = fminsearch(@curvefit, [1,1], options, dose, D_eff(:,i));
end
And I get an error displaying "In an assignment A(I) = B, the number of elements in B and I must be the same."
If it take out the (i) after best_fit_eff, it would run properly, but then Matlab would only retain the best fit of the 7th iteration, but I want a matrix with the best fit values of all the iterations. Can anyone help me please?

Accepted Answer

Niraja
Niraja on 5 Dec 2014
I have actually had this very question not too long ago. What you have to do here is create another variable in the for loop and store the value of best_fit_eff into the array after every iteration. Here is code that you could possibly use to get around this problem. Also, it looks like your best_fit_eff outputs a vector with two values rater than one, so in the code below, I assigned the first value to a variable called "param1" and the second value to "param2". This should work without any errors. Let me know if it doesn't!
options = optimset();
for i=1:7
best_fit_eff = fminsearch(@curvefit,[1,1], options, dose, D_eff(:,i));
param1(i) = best_fit_eff(1);
param2(i) = best_fit_eff(2);
end

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!