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

functionimagematrixsize;

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.

Best Answer

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.