How to print size of array?

251 vues (au cours des 30 derniers jours)
John
John le 11 Juil 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?

Réponses (2)

Steven Lord
Steven Lord le 11 Juil 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 le 11 Juil 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 commentaires
John
John le 11 Juil 2017
Dimension is unknown. That's the problem!
Geoff Hayes
Geoff Hayes le 11 Juil 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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by