如何在Octave中输出矩阵?

3
我有两个文件,第一个是主文件,它调用第二个文件。因此,在运行第一个文件时,我想输出在第二个文件中创建的矩阵,也许可以通过主文件中的printf输出?
我已经尝试过这种方法,但没有成功,而且显示的是行而不是列:
    printf("[%f; %f; %f]\n",r)

我不理解你的问题,特别是“主文件和次文件”的部分。你是指调用另一个脚本文件的脚本文件吗?为什么不使用函数并像通常一样返回矩阵?顺便说一下,你可以通过调用 disp(yourmatrix) 简单地输出矩阵。 - Andy
是的,两个都是脚本文件... 我不知道你所说的“通常”是什么意思?我在Octave中是新手,我已经尝试在两个文件中使用disp(yourmatrix),但没有成功... 我只想打印矩阵以进行调试... - isk27
2个回答

4

如果你想要调试输出(尤其是在循环内部),你需要先使用more off命令关闭分页,然后使用disp(当然你需要在这里添加你的矩阵名称)或者只是提及变量而不加分号,或者在赋值时去掉分号。

more off
for k=1:2
  a = rand(2) * k;  # remove trailing ;
  a                 # or mention it without ;
  disp (a)          # or use disp which doesn't show the variable name
endfor

输出

a =

   0.80112   0.53222
   0.48930   0.56336

   0.80112   0.53222
   0.48930   0.56336
a =

   1.30374   1.85382
   0.30519   0.42486

   1.30374   1.85382
   0.30519   0.42486

请注意,变量a被显示了两次:一次是带有"a = "的,一次是没有的。


4
在您的当前文件夹中创建以下文件。
%%% in file: second.m

A = [1,2;3,4];    % adding a semicolon suppresses the output

%%% in file: master.m

% run 'second.m' script - this will add A on the workspace, since
% running this script is as if you had dumped the file's contents here
second    

% call the value of A without a semicolon to show contents on screen
A         

Then from your octave terminal, run the 'master.m' script:

master

This will display the contents of A on screen.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接