doing polyvar giving two conditions

1 次查看(过去 30 天)
i have a matrix xx(100*1) of elements less than and greater than 70 and a y matrix.p1=polyfit(xx,y,1)when xx<70 and p2=polyfit(xx,y,1);when xx>70 . i need to polyvar(p1,xx)for xx<70 and polyvar(p2,xx)for xx>70 and get the two condition as a single matrix. i tried with if loop but getting answer only for the else condition.is it possible to get answer in both conditions?

采纳的回答

Star Strider
Star Strider 2016-1-16
See if this does what you want:
xx = linspace(1, 140); % Create Data
y = randi(99, 1, 100); % Create Data
p1 = polyfit(xx(xx<70), y(xx<70), 1);
p2 = polyfit(xx(xx>=70), y(xx>=70), 1);
y1 = polyval(p1,xx(xx<70));
y2 = polyval(p2,xx(xx>=70));
figure(1)
plot(xx(xx<70), y1, xx(xx<70),y(xx<70))
hold on
plot(xx(xx>=70), y2, xx(xx>=70),y(xx>=70))
hold off
grid
  2 个评论
seema niran
seema niran 2016-1-16
yes upto y1 and y2 ,it is correct .but i need y1 and y2 as a single matrix y such that y contain element of y1 for xx<70 and y2 for xx>70
Star Strider
Star Strider 2016-1-16
Call the matrix (actually vector) containing ‘y1’ and ‘y2’, ‘yfit’ (or something more original), and since both are row vectors, define it as:
yfit = [y1 NaN y2];
If you want to define ‘xxfit’ in the same way (albeit redundantly in my example):
xxfit = [xx(xx<70) NaN xx(xx>=70)];
You could then plot them as:
figure(2)
plot(xx, y, 'bp') % Plot Original Data
hold on
plot(xxfit, yfit, '-r') % Plot Fitted Regression Lines
hold off
grid
The NaN values are to break up the regression lines and to make both vectors have equal lenghs.
Providing that your independent variables are well-behaved (such as ‘xx’ is here) you can do your regressions as I have done them here. If they are discontinuous or have extremely high magnitudes, you would have to centre and scale them to get the best and most robust fit. That involves adding the ‘S’ and ‘mu’ outputs to your polyfit calls and using them as well in your polyval calls. This is inconvenient, but will result in reliable parameter estimates and a reliable fit to the data. See the documentation for the functions for a full explanation.
And if you really want to ‘gild the lily’ and report the confidence intervals for the parameters ‘p1’ and ‘p2’, use the absolutely brilliant File Exchange contribution polyparci. Try it! you’ll like it!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Preprocessing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by