gpt4 book ai didi

matlab - 在matlab中将包含二进制数据的文本文件读取为单个矩阵

转载 作者:太空宇宙 更新时间:2023-11-03 19:16:52 25 4
gpt4 key购买 nike

我有一个包含二进制数据的文本文件,格式如下:

00000000000000000000000000000000001011111111111111111111111111111111111111111111111111111111110000000000000000000000000000000
00000000000000000000000000000000000000011111111111111111111111111111111111111111111111000111100000000000000000000000000000000
00000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111100000000000000000000000000000000
00000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000
00000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111100000000000000000000000000000000
00000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111100000000000000000000000000000000
00000000000000000000000000000000000000011111111111111111111111111111111111111111111111000111110000000000000000000000000000000
00000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111110000000000000000000000000000000
00000000000000000000000000000000000000000000111111111111111111111111111111111111110000000011100000000000000000000000000000000
00000000000000000000000000000000000000011111111111111111111111111111111111111111111111100111110000000000000000000000000000000
00000000000000000000000000000000000111111111111111111111111111111111111111111111111111110111110000000000000000000000000000000
00000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000
00000000000000000000000000000000000000001111111111111111111111111111111111111111111111000011100000000000000000000000000000000
00000000000000000000000000000000000000001111111111111111111111111111111111111111111111000011100000000000000000000000000000000
00000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111000000000000000000000000000000000
00000000000000000000000000000000000000011111111111111111111111111111111111111111111110000011100000000000000000000000000000000
00000000000000000000000000000000000000000000011111111111111111111111111111111111100000000011100000000000000000000000000000000
00000000000000000000000000000000000000111111111111111111111111111111111111111111111111110111100000000000000000000000000000000

请注意,每个 1 或 0 都是独立的,即值不是小数。我需要找到文件的按列总和。一共125列,840946行。

我已经尝试过 textreadfscanf 和其他一些 matlab 命令,但结果是它们都以十进制格式读取每一行并创建一个 840946x1 数组。我想创建一个 840946x125 矩阵来计算按列求和。

最佳答案

您可以使用 textread 来完成。只需读取字符串,然后使用 sscanf 处理它们,一次一位数

A = textread('data.txt', '%s');
ncols = size(A, 1);
nrows = size(A{1}, 2);
A = reshape(sscanf([A{:}], '%1d'), nrows, ncols);

请注意,现在 A 已转置,即您有 125 行。

然后按列计算总和

colsum = sum(A);

关于matlab - 在matlab中将包含二进制数据的文本文件读取为单个矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12705697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com