Input with a count

4 ビュー (過去 30 日間)
Corey K
Corey K 2015 年 1 月 29 日
コメント済み: Star Strider 2015 年 1 月 29 日
This is what I have so far
while (nargin==0)
numpts = input('Please enter in the number of points:');
fprintf('Please enter in points in [x y] form.')
for count=1:numpts
xy_points = input('Enter [x y] pair for point %g:', count);
end
x_points = xy_points(:,1);
y_points = xy_points(:,2);
end
This is the error
Error using input
The second argument to INPUT must be 's'.
Error in Untitled (line 30)
xy_points = input('Enter [x y] pair for point %g:', count);
and these are the directions for the assignment Write a MATLAB function (not script) that performs a linear regression fit on a set of data. The function should be able to handle two cases:
1. Two input arguments (a vector of x points and a vector of y points), or
2. Zero input arguments, the program should first:
a. Ask the user to input the number of points they will enter
b. Enter (x,y) points one-by-one
The output should return and display the straight-line slope, intercept, and number of points that were used. The program should also graph the data points as discrete (not connected by a line) circles, and on the same axis the straight-fit line ranging across the x values.
  1 件のコメント
Hikaru
Hikaru 2015 年 1 月 29 日
編集済み: Hikaru 2015 年 1 月 29 日
I think you have to take a look at this part.
for count=1:numpts
xy_points = input('Enter [x y] pair for point %g:', count);
end
You're basically assigning new value to xy_points in each iteration, which is not what you want. Why don't you try making xy_points into a matrix of 2 columns first?
xy_points = zeros(numpts,2); % this is called pre-allocating
Now, can you write the code to tell MATLAB to assign the user input into the variable xy_points? Hint: This is where you could use your count variable.

サインインしてコメントする。

回答 (1 件)

Star Strider
Star Strider 2015 年 1 月 29 日
See the documentation for input. (I prefer to use the inputdlg function because it avoids Command Window pollution.)
  6 件のコメント
c k
c k 2015 年 1 月 29 日
編集済み: Star Strider 2015 年 1 月 29 日
I just gave up on trying to put the %g part.
This is what I have in my code now and it will show in the command window to enter the number of points, then will ask to enter a point that many times, but from then on it just repeats itself, as if by terms of the while loop it still has nargen==0. Any insight into this?
function[fit_slope, fit_intercept, Npoints] = linear_regression(x_points,y_points)
...
while (nargin==0)
numpts = input('Please enter in the number of points:');
fprintf('Please enter in points in [x y] form.')
for count=1:numpts
xy_points = input('Enter [x y] pair for point');
x_points = xy_points(:,1);
y_points = xy_points(:,2);
end
end
% % plot x versus y
% plot(x_points,y_points);
% % create matrix
% M=[numpts,sum(x_points);sum(x_points),sum(x_points.^2)];
% %b is y-intercept and n is the slope
% bn=inv(M)*[sum(y_points);sum(x_points.*y_points)]
% %display line equation
% disp('f(x)=nx+b')
Star Strider
Star Strider 2015 年 1 月 29 日
Forget the while loop. It isn’t necessary. I would simply test for ‘nargin == 0’, and return if true (perhaps first printing an error message to the Command Window).
The problem statement is confusing as I read it. My impression is that your script will ask for the user to enter the ‘x_points’ and ‘y_points’ vectors from the input (or inputdlg) assignments, then passes them to your regression function that then returns the required output. Your script (not the function) would then take the function output and do the plot. You might want to clarify that. That is how I understand the assignment, anyway.
I would also use the backslant (\) operator to do the regression. For example:
x_points = [11:3:39]'; % Column Vector
y_points = [1:10]'; % Column Vector
B = [ones(size(x_points)) x_points]\y_points;
fit_intercept = B(1);
fit_slope = B(2);
Your ‘x_points’ and ‘y_points’ vectors are already column vectors as you wrote your code, so you can use them as they are in this code snippet.
Also, the way I read the problem statement, it seems to want you to get ‘x_points’ and ‘y_points’ before you call your regression function, not from within it, since you are supposed to pass them to it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by