How to print size of array?

227 次查看(过去 30 天)
John
John 2017-7-11
The array size need to monitor. It's simple and effective with size(x) but no name or location. It would be nice to print if out this way:
fprintf(' size(xyz) at location 123 is [%d %d %d] \n',size(xyz));
because size(xyz) is "unknown" and how to write [%d %d %d] to print size(xyz) in one row?

回答(2 个)

Steven Lord
Steven Lord 2017-7-11
>> A = rand(2, 3, 1, 4);
>> fprintf('size(A) is %s\n', mat2str(size(A)))
or
>> A = rand(2, 3, 1, 4);
>> fprintf('size(A) is [%s]\n', int2str(size(A)))
Note that the spacing between the elements of the size may be a little different for those two approaches.

Geoff Hayes
Geoff Hayes 2017-7-11
John - is xyz your 3D array? If so, then try
fprintf(' size(xyz) at location 123 is [%d %d %d] \n',size(xyz, 1), size(xyz, 2), size(xyz, 3));
  2 个评论
John
John 2017-7-11
Dimension is unknown. That's the problem!
Geoff Hayes
Geoff Hayes 2017-7-11
then consider using ndims with
xyz = randi(255,12,13,14);
fprintf('size(xyz) at location 123 is [');
for k=1:ndims(xyz);
fprintf(' %d',size(xyz, k));
end
fprintf(']\n');
or
fprintf('size(xyz) [');
fprintf('%d ',size(xyz)');
fprintf(']\n');
Or do you have a requirement that stats that you can only print this in one line?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by