linux - Octave 高级 textread 用法,bash

标签 linux bash octave

我有以下文本文件:

079082084072079032084069067072000000000,0 082078032049050032067072065082071069000,1 076065066032065083083084000000000000000,0 082078032049050072082000000000000000000,1 082078032049050072082000000000000000000,1 082078032049050072082000000000000000000,1 070083087032073073032080068000000000000,0 080067065032049050032072082000000000000,0 082078032056072082000000000000000000000,1 070083087032073073073000000000000000000,0 082078032087069069075069078068000000000,1 082078032049050072082000000000000000000,1 077065073078084032077069067072032073073,0 082078032049050072082000000000000000000,1 080067065032049050032072082000000000000,0 082078032049050072082000000000000000000,1

我也需要矩阵: X 尺寸 16x13 Y 尺寸 16x1

我想把文件的每一行分成13个值,例子: 079 082 084 072 079 032 084 069 067 072 000 000 000

是否可以使用textread函数将其导入octave?

如果不是,是否可以使用 Linux bash 命令完成?

最佳答案

是的,您可以使用 textscan 来完成此操作(如果您真的想使用 textread,请参阅底部:

octave> txt = "079082084072079032084069067072000000000,0\n082078032049050032067072065082071069000,1";
octave> textscan (txt, repmat ("%3d", 1, 13))
ans = 
{
  [1,1] =

    79
    82

  [1,2] =

    82
    78

  [1,3] =

    84
    32

  [1,4] =

    72
    49
[...]

请注意,您将它们读取为数值,因此不会得到前面的零。如果您需要它们,您可以通过在格式中使用“%3s”将它们读取为字符串(处理起来会很麻烦并且性能会降低,因为您随后将处理元胞数组)。

由于您正在读取文件:

[fid, msg] = fopen ("data.txt", "r");
if (fid)
  error ("failed to fopen 'data.txt': %s", msg);
endif 
data = textscan (fid, repmat ("%3d", 1, 13));
fclose (fid);

如果你真的想使用textread:

octave> [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13] = textread ("data.txt", repmat ("%3d", 1, 13))
d1 =

  79
  82
  76
[...]    
d2 =

  82
  78
  65
[...]

或:

octave> data = cell (1, 13);
octave> [data{:}] = textread ("data.txt", repmat ("%3d", 1, 13))
data = 
{
  [1,1] =

    79
    82
    76
[...]

  [1,2] =

    82
    78
    65
[...]

如果您需要捕获逗号后的值(实际上不是您原始问题的一部分),您可以使用:

octave> textscan (txt, [repmat("%3d", 1, 13) ",%1d"])
ans = 
{
  [1,1] =

    79
    82

  [1,2] =

    82
    78

  [1,3] =

    84
    32

[...]

  [1,14] =

0
1

关于linux - Octave 高级 textread 用法,bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32387358/

相关文章:

gnuplot - 使用不同颜色在 3D 中绘制 block 数据并在 Gnuplot 或 Octave 中平滑线条

Matlab/Octave 将二维图导出为图像而不显示

gnuplot - GNU Octave报告: unknown or ambiguous terminal type on OS X [duplicate]

linux - 排序上次登录时间

bash - 将 grep 重定向到搜索目录中的文件会导致无限递归

linux - 从命令提示符更新 Python 版本以及从 PyQt4 到 PyQt5 的转换

python - 将 Python 脚本的返回值存储在 bash 脚本中

node.js - 令我害怕的 sh 线,它便携吗?

linux - 有没有办法读取 cgroups 用来终止进程的内存计数器?

java - (为什么)Tomcat/Java 在 Linux 上比在 Windows 上表现更好?