Estimating polyval in a for loop
Show older comments
Hello,
I have a data of x and Y values which I call Yobserved and Xobserved which follow a simple linear equation model: y = ax + b. I am trying to estimate calculated values of Y which I call Ycalc from already obtained values of a and b.
a and b values are the intercepts and slopes of these straight lines. In length they are 11. so I have a 2 by 11 matrix with 1st column b and 2nd column, a. Now for each pair of a and b value and the observed Y values. I am trying to estimate the predicted Y values of using polyval.
PROBLEM: Calculated Y values from polyval can be of ANY LENGTH for each pair of a and b. I am doing this in a for loop and only the first value of Y gets returned which is not supposed to be the case.
Y value is meant to vary in length since the data that produced a and b were selected according to a criteria. That means Y could be e.g 200X1, or 505X1, etc. Y is a single vector of variable length not a matrix. I just need all these vectors in one matrix.
I cannot predetermine the length of Y unless it has been calculated by polyval making preallocation a little challenging.
Now my question is : how do i return the output of "polyval" i.e Y in the loop since the size of Y changes inside the loop.
I know this is a problem of variable size vectors but not sure how to go about it. Any suggestions would really be of help. Thanks
Here's my code.
MC = [1:0.1:2.0]'; %
n = length(MC);
for i = 1:length(MC);
ss = find(ML>=MC(i));
Msel = ML(ss); % selected data range for which to calculate a and b values
% calculate b value, avalue and uncertainty in b
[bvalue(i),stdv(i),aval(i)] = bmaxLE(ML,MC(i)); % function to calculate a and b values
hh = [-1*bvalue(i),aval(i)]% put a and b in a matrix
Y = 10.^(polyval(hh,[1,3.2]));% calculate the predicted Y values for each set of a and b
C1{i} = Y;% put each vector of Y in a cell
end
Answers (1)
Walter Roberson
on 30 Nov 2013
After the loop,
Y = vertcat(C1{:});
If I scan your code correctly, this should result in a something-by-2 matrix. If you want a single row vector instead, switch to horzcat()
Categories
Find more on Linear Algebra 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!