How to print size of array?

227 visualizzazioni (ultimi 30 giorni)
John
John il 11 Lug 2017
Risposto: Steven Lord il 11 Lug 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?

Risposte (2)

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

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by