How to print size of array?

227 visualizaciones (últimos 30 días)
John
John el 11 de Jul. de 2017
Respondida: Steven Lord el 11 de Jul. de 2017
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?

Respuestas (2)

Steven Lord
Steven Lord el 11 de Jul. de 2017
>> 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 el 11 de Jul. de 2017
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 comentarios
John
John el 11 de Jul. de 2017
Dimension is unknown. That's the problem!
Geoff Hayes
Geoff Hayes el 11 de Jul. de 2017
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?

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by