使用 load 代替其他 I/O 命令

发布于 2024-08-31 03:21:32 字数 1017 浏览 4 评论 0原文

如何使用 load-ascii 命令修改此程序来读取 (x,y)?

n=0;
sum_x = 0;
sum_y = 0;
sum_x2 = 0;
sum_xy = 0;
disp('This program performs a least-squares fit of an');
disp('input data set to a straight line. Enter the name');
disp('of the file containing the input (x,y) pairs:  ');
filename = input('   ','s');
[fid,msg] = fopen(filename,'rt');
if fid<0
    disp(msg);
else
    [in,count]=fscanf(fid, '%g %g',2);
    while ~feof(fid)
        x=in(1);
        y=in(2);
        n=n+1;
        sum_x=sum_x+x;
        sum_y=sum_y+y;
        sum_x2=sum_x2+x.^2;
        sum_xy=sum_xy+x*y;
        [in,count] = fscanf(fid, '%f',[1 2]);
    end
    fclose(fid);
    x_bar = sum_x / n;
    y_bar = sum_y / n;
    slope = (sum_xy - sum_x*y_bar) / (sum_x2 - sum_x*x_bar);
    y_int = y_bar - slope * x_bar;
    fprintf('Regression coefficients for the least-squares line:\n');
    fprintf('Slope (m)      =%12.3f\n',slope);
    fprintf('Intercept (b)  =%12.3f\n',y_int);
    fprintf('No of points   =%12d\n',n);
end

How can I modify this program using load-ascii command to read (x,y)?

n=0;
sum_x = 0;
sum_y = 0;
sum_x2 = 0;
sum_xy = 0;
disp('This program performs a least-squares fit of an');
disp('input data set to a straight line. Enter the name');
disp('of the file containing the input (x,y) pairs:  ');
filename = input('   ','s');
[fid,msg] = fopen(filename,'rt');
if fid<0
    disp(msg);
else
    [in,count]=fscanf(fid, '%g %g',2);
    while ~feof(fid)
        x=in(1);
        y=in(2);
        n=n+1;
        sum_x=sum_x+x;
        sum_y=sum_y+y;
        sum_x2=sum_x2+x.^2;
        sum_xy=sum_xy+x*y;
        [in,count] = fscanf(fid, '%f',[1 2]);
    end
    fclose(fid);
    x_bar = sum_x / n;
    y_bar = sum_y / n;
    slope = (sum_xy - sum_x*y_bar) / (sum_x2 - sum_x*x_bar);
    y_int = y_bar - slope * x_bar;
    fprintf('Regression coefficients for the least-squares line:\n');
    fprintf('Slope (m)      =%12.3f\n',slope);
    fprintf('Intercept (b)  =%12.3f\n',y_int);
    fprintf('No of points   =%12d\n',n);
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2024-09-07 03:21:32

如果我猜对了,也许你想要这个:

x=load('-ascii',filename);
sumx =sum(x(:,1));
sumy =sum(x(:,2));
sumx2=sum(x(:,1).^2);
sumy2=sum(x(:,2).^2);
sumxy=sum(x(:,1).*x(:,2));
n    =size(x,1);

也试试这些

sums =sum(x);
sum2s=sum(x.^2);
sumxy=sum(prod(x,2));
means=mean(x);

你也知道有一个函数

[slope,int]=regress(x(:,2), [x(:,1) ones(n,1)])

可能会消除你的工作:)

编辑

鉴于你在下面放置的内容,你的数据似乎不在单独的行中,并且由仅用空格分隔的数字组成。因此,如果这是正确的,则在 load 语句之后,您应该插入以下行:

x=reshape(x, 2, length(x)/2);

注意,读取 ASCII 文件时,准确的格式很重要!请注意,如果您的数字数量为奇数,则此操作将会失败;如果文件中有任何换行符,则此操作将给出错误的值。

我认为您编写的所有其余代码都可以简化为:

x=load('-ascii',filename);
[slope,int]=regress(x(:,2), [x(:,1) ones(size(x,1),1)])

If I get you right, perhaps you want this:

x=load('-ascii',filename);
sumx =sum(x(:,1));
sumy =sum(x(:,2));
sumx2=sum(x(:,1).^2);
sumy2=sum(x(:,2).^2);
sumxy=sum(x(:,1).*x(:,2));
n    =size(x,1);

Also try these

sums =sum(x);
sum2s=sum(x.^2);
sumxy=sum(prod(x,2));
means=mean(x);

Also do you know that there is a function

[slope,int]=regress(x(:,2), [x(:,1) ones(n,1)])

which might obviate your work :)

edit

Given what you have put below, it appears that your data is not in separate rows, and consists of numbers separated by spaces only. So if this is correct, after the load statement, you should insert the line

x=reshape(x, 2, length(x)/2);

Note that when reading an ASCII file, the exact format is important! Please note that this will fail if you have an odd number of numbers, and will give incorrect values if you have any newline characters in your file.

I think that all the rest of the code you have written reduces to:

x=load('-ascii',filename);
[slope,int]=regress(x(:,2), [x(:,1) ones(size(x,1),1)])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文