Search code examples
matlaboctave

how to print the size of a matrix in a print statement?


I want to print the size of a matrix/vector in a text print statement. Something like this:

fprintf("The size of the matrix is: %s", size(m))

of course it didn't work and I'm unable to find out how.

Edit:
I tried this and it worked, but is there a better way to do it?

fprintf('The size of the matrix is: %s\n', num2str(size(p)))

Solution

  • In fprintf, %s is used for strings. You can use num2str or any of the solutions provided.

    fprintf('The size of the matrix is: %s', num2str(size(zeros(10,10))))
    

    Edit 10 years later: A much nicer way:

    fprintf('The size of the matrix is: %s', regexprep(num2str(size(zeros(10,11,12))),'\s{1,}','×'))
    

    Returns:

    The size of the matrix is: 10×11×12