AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79224033
Accepted
Varga
Varga
Asked: 2024-11-26 01:22:05 +0800 CST2024-11-26 01:22:05 +0800 CST 2024-11-26 01:22:05 +0800 CST

计算一组 excel 文件的行平均值时出错

  • 772

有一组 excel 文件(10 个文件),每个文件都有一个表格:2 列(参数名称、参数值)和 12 行(参数数量及其值)。需要计算单个参数的 10 个变量之间的平均值。下面有一个程序可以实现此目的。但是,我注意到他执行计算时的误差为万分之一,他低估了它。例如,结果如下:

矩阵 使用 Excel 计算平均值
0.0288 0,02891568
0.0008 0,000836121
0.0012 0,00117159
0.0723 0,07248086
0.0858 0,085798107
0.0867 0,086977032
0.1030 0,102957729
0.9524 0,959947104
-0.0050 -0,005002288
0.0008 0,000837283
-0.0000 -1,28168E-05
0.0000 2,17761E-06
clc, clear;
 
% Folder with Excel files
folder = 'The path to the folder with 10 excel files'; % the path to the folder
files = dir(fullfile(folder, '*.xlsx')); % we get a list of all excel files
 
% Initialize the vector to store the average values
average_values = [];
 
% Loop through all files
for i = 1:length(files)
    % The full path to the file
    file_path = fullfile(folder, files(i).name);
 
    % Reading data from an Excel file
    opts = detectImportOptions(file_path, 'VariableNamingRule', 'preserve'); 
    data = readtable(file_path, opts);
 
    % Check if there is at least one numeric column
    if any(varfun(@isnumeric, data, 'OutputFormat', 'uniform'))
        % We extract only numeric data
        numeric_data = data{:, varfun(@isnumeric, data, 'OutputFormat', 'uniform')}; 
 
        % We calculate the average values for the rows, ignoring NaN
        avg_values_per_row = mean(numeric_data, 2, 'omitnan');
 
        % Saving the average values in an array
        average_values = avg_values_per_row;
    else
        warning('There are no numeric columns in the "%s" file.', files(i).name);
    end
end
 
% Displaying average values
disp('Average values by line:');
disp(average_values);
 
% Displaying the number of rows
disp('Number of lines:');
disp(length(average_values));

如您所见,对于参数编号 1、4、6、8,低估值从 0.0001 到 0.0007 个单位(对于第 8 个参数,低估值已经是该数字的千分之一)。最有趣的是,如果您手动将每个参数的 10 个值输入meanMatlab 中的函数并进行计算,它将给出正确的结果,而不会出现这种低估(使用第 8 个参数的示例):

>> mean([0.952380002    0.980380905 0.97087088  0.95237715  0.952379051 0.970871364 0.980383351 0.925921306 0.961536062 0.952370972
])

ans =

    0.9599

代码中哪里可能存在问题以及如何修复它以便给出准确的结果?(附件包含一个带有示例文件的文件夹: https: //drive.google.com/file/d/1COPpBq2mU8Ww62DRvbOKTtr_aC-LsCIL/view? usp=sharing )

excel
  • 1 1 个回答
  • 35 Views

1 个回答

  • Voted
  1. Best Answer
    rotabor
    2024-11-26T04:18:44+08:002024-11-26T04:18:44+08:00

    您的循环仅返回上一个工作簿中的值。打开“Param10.xlsx”并将值与您在问题中显示的平均值进行比较。错误在这里:

    for i = 1:length(files)
            ...
            average_values = avg_values_per_row;
            ...
    end
    

    每次循环运行时,average_values 都会获取当前值。正如我之前所写,您需要像这样求和(不知道确切的语法):

            average_values = sum(average_values,numeric_data)
    

    循环结束后,将 average_values 的每个元素除以 'length(files)'。

    • 1

相关问题

  • 如何返回列出的合同上有费率但系统中没有费率的特定行?

  • 当某些值重复时自动在表中添加参考字段?

  • 循环遍历具有更改单元格地址的列

  • 搜索字符串并输出与该字符串对应的值

  • Excel中有没有一种方法可以计算字符串中特定文本的出现次数,但也包括前一个字符?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve