About size function -Syntax : [m,n] = size(X)

12 views (last 30 days)
Hello, I wrote this code, but what does the number 1500 means?
i searched about 'size'through 'help' and they say '[m,n] = size(x)' returns the size of matrix X. But the size of the image I used is 300(height)*500(length).
Why the first l is 1500?
Thanks.

Accepted Answer

Stephen23
Stephen23 on 26 Jul 2016
Edited: Stephen23 on 26 Jul 2016
This often confuses beginners who do not bother to read the documentation.
Explanation
The important thing is that the last output variable is the product of all remaining dimensions, as the size documentation clearly states: "...but dn equals the product of the sizes of the remaining dimensions of X, that is, dimensions n through ndims(X)."
Beginners also forget that an image is (usually) not a 2D matrix, but is a 3D array. So you have a 3D array (RGB image), and are returning only two outputs... the docs clearly explain that the second output will be the product of dims two and three: rows*3 == 500*3 == 1500 (because the third dimension encodes RGB, so has size 3).
Solutions
1. One solution is to use three outputs (for a 3D array):
[rows,cols,pages] = size(...)
Where pages corresponds to the Red, Green, and Blue "pages" (or layers) of the image.
2. Use just one output:
>> size(ones(2,3,4))
ans =
2 3 4
3. Specify the dimension being measured:
>> R = size(ones(2,3,4),1)
R = 2
>> C = size(ones(2,3,4),2)
C = 3
Further Reading
This topic has been on this forum and in the MATLAB blogs:
You can test it too:
>> [R,Z] = size(ones(2,3,4)) % Z is product of dims two and three
R = 2
Z = 12
>> [R,C,P] = size(ones(2,3,4)) % each dim is returned separately.
R = 2
C = 3
P = 4
The moral of the story: read the documentation.
  1 Comment
Guillaume
Guillaume on 26 Jul 2016
To add to Stephen's answer about images. Images in matlab can be of several types.
  • Binary (black and white) and greyscale images are stored as 2D matrices (row x column storing pixel intensity).
  • Indexed images are also stored as 2D matrices but have an associated colour map (row x column storing index into colour map).
  • Finally, RGB images are stored as 3D matrices (row x column x colour plane) where each plane is a colour channel. Note that an RGB image can appear greyscale if all three channels are equal.
PNG images can directly store greyscale, indexed, or rgb images. If you don't know which one is stored in your file, you can query it with imfinfo.
You can also check the number of dimensions of the matrix returned by imread with ndims

Sign in to comment.

More Answers (1)

KSSV
KSSV on 26 Jul 2016
size() gives you number of rows and column of a given matrix... imread() reads an image into pixels..it outputs RGB values if the image is coloured.... In your case the image you read has size of 300x1500
  2 Comments
Stephen23
Stephen23 on 26 Jul 2016
Edited: Stephen23 on 26 Jul 2016
This Answer is Wrong and Misleading
The image has size 300x500x3
Read the size documentation and my answer to know why.
Guillaume
Guillaume on 26 Jul 2016
Indeed, very very wrong answer!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!