How to print size of array?

조회 수: 225 (최근 30일)
John
John 2017년 7월 11일
답변: Steven Lord 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