MATLAB: 从 CSV 文件中提取 3 的倍数行到新数据表
以下代码示例展示了如何从原始数据表中读取 3 的倍数行数的数据,并将其存储到一个新的数据表中:
% 读取原始数据表
data = readtable('original_data.csv');
% 获取3的倍数行数的索引
indices = mod(1:size(data,1),3) == 0;
% 选择3的倍数行数的数据
new_data = data(indices,:);
% 将新数据表写入文件
writetable(new_data,'new_data.csv');
代码解释:
- 读取原始数据表: 使用
readtable函数读取名为 'original_data.csv' 的数据表,该数据表包含需要处理的数据。 - 获取 3 的倍数行数的索引: 使用
mod函数计算每一行的索引是否是 3 的倍数,得到一个布尔型向量indices。 - 选择 3 的倍数行数的数据: 使用
indices向量选择原始数据表中对应的行,得到一个新的数据表new_data,该数据表只包含 3 的倍数行数的数据。 - 将新数据表写入文件: 使用
writetable函数将新数据表new_data写入名为 'new_data.csv' 的文件中。
原文地址: https://www.cveoy.top/t/topic/npjL 著作权归作者所有。请勿转载和采集!