本文介绍了如何输出矩阵尺寸及其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使GNU Octave输出矩阵尺寸及其内容?例如,它应该产生污秽.像这样:
Is it possible to make GNU Octave to output matrix dimensions together with its content? For example, it should produce smth. like this:
octave:1> X = [1 2; 3 4]
X [2x2] =
1 2
3 4
octave:2> X(1,:)
ans [1x2] =
1 2
推荐答案
在MATLAB中,使用以下内容在路径中某处名为@double
的文件夹中创建display.m
:
In MATLAB, create display.m
in a folder called @double
somewhere in your path with this content:
function display(v)
name = inputname(1);
if isempty(name)
name = 'ans';
end
s = num2cell(size(v));
fprintf('\n%s [%d%s] =\n\n', name, s{1}, sprintf('x%d', s{2:end}));
builtin('disp', v);
end
这样,您将覆盖类double
的display
方法,并完全获得您所描述的内容.但是,这对于其他类(例如int8
,logical
或cell
)不起作用.您必须对所有感兴趣的类都重写该方法.示例:
This way you override the display
method for class double
, and get exactly what you have described. However, this will not work for other classes like int8
, logical
or cell
. You have to override the method for all classes you are interested in. Example:
>> A=ones(2,2,2)
A [2x2x2] =
(:,:,1) =
1 1
1 1
(:,:,2) =
1 1
1 1
这篇关于如何输出矩阵尺寸及其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!