what's the second variable in size function do ?

Hi,
I am little confused about the size function. please see the attached. for example,
let's say f= [ 4 3 5 6 ]
now if I write size(f,1) I get f = 1
if I write size (f,2) I get f = 4
if I write size (f,3) I get f = 1
if I write size ( f,4) I get f = 1
My question what does the number 1,2,3,4 mean/do in the size function mentioned above ? Thanks

 채택된 답변

James Tursa
James Tursa 2018년 1월 5일

28 개 추천

The second argument to the size function designates the particular dimension that you want. E.g., in your example, f is a 1x4. The first dimension is 1, the second dimension is 4. That is why size(f,1) returns 1 and size(f,2) returns 4. For the trailing dimensions that are not physically present in a variable, size returns 1 by convention. So size(f,3) returns 1, size(f,4) returns 1, and size(f,20) would return 1 also.

댓글 수: 5

This was useful for me. Thank you very much!
@ James Tursa
Dear Sir,
Still I didn't this idea, kindly help.
Let's make an array in MATLAB.
X = reshape(1:120, [2 3 4 5]);
This array X has two rows, three columns, four pages, and five "books" (seems as good a name for the 4th dimension as any, since it extends the pattern of the 3rd dimension name. We could use "shelf" as the name for the 5th dimension and "library" for the 6th.) X is a 4-dimensional array.
>> ndims(X) % return 4
The size of X in the first dimension is the number of rows, in this case 2.
>> size(X, 1) % returns 2
The size of X in the third dimension is the number of pages, in this case 4.
>> size(X, 3) % returns 4
The size of X in the fifth dimension is the number of "shelves". We haven't specified how many shelves X spans. In MATLAB, arrays have a (large but finite) number of implicit trailing singleton dimensions. if you ask for the size of an array in one of those trailing singleton dimensions, the answer is 1. MATLAB will not display those trailing singleton dimensions when you call size with one input or if you ask for ndims, but they can be used for certain purposes (asking for the size in a specific dimension and implicit expansion being two of the major uses.)
>> size(X, 5) % returns 1, since X is 4-dimensional
Karthik B
Karthik B 2020년 4월 12일
thanks for the answer, i found it useful
Thank you soo much,was confused by this for a bit

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

faw3b
faw3b 2018년 1월 5일

7 개 추천

The function returns the length of the specified dimension (Take a look at the function Documentation). The array f is an 1x4 array. So its first dimension is 1, the second 4.
If you define another array with different dimensions (2x3 array)
A = [5 7 8;
0 1 9];
the function returns
size(A,1)
ans =
2
and
size(A,2)
ans =
3
the array dimensions. A two dimensional array with three elements each.

댓글 수: 2

many thanks :)
I have a followup. What does this mean?
eta = (X' - repmat(constant,size(X,1),1)' where:
X' = (652X3)' or (3X652), X = (652X3), constant = [1X3], lets say it contains [5 2 1].
I assume size (X,1) is first dim of X or '652'.
Then this eta = 3X652 - repmat [(1X3),652,1)]' ?'
Q1 The second part is a [(1X3) with 652 rows and 1 column] transposed?
Let's say it's like:
[5 2 1]
[5 2 1]
...
[5 2 1] (652th row)?
Q2. Then its transpose is:
[5 2 1] [5 2 1] ... [5 2 1} for 652 columns?
Q2 How is this possibly subtracted from X' = [652X3]?
Q3 Or is the transpose somehow:
[5
2 for 652 columns, so it's 3 X 652 as well?
1]
-----
3D arrays a bit confusing. Thanks in advance.
T

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2018년 1월 5일

댓글:

2021년 11월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by