four dimensions array's expression
조회 수: 7 (최근 30일)
이전 댓글 표시
A=rand(2,3,3)
B=cat(4,A(:,:,1),A(:,:,2),A(:,:,3))
B(:,:,1,1)=
B(:,:,1,2)=
B(:,:,1,3)=
B(:,:,1,4)=
Why not
B(:,:,1,1)=
B(:,:,2,1)=
B(:,:,3,1)=
B(:,:,4,1)=
댓글 수: 0
답변 (2개)
per isakson
2019년 4월 3일
>> size(B)
ans =
2 3 1 3
the statement
B = cat(4,A(:,:,1),A(:,:,2),A(:,:,3))
concatenates three 2D arrays along the fourth dimension. I don't think this case is described in the documentation. However, Matlab obviously adds a third dimension of size one.
Stephen23
2019년 4월 3일
편집: Stephen23
2019년 4월 3일
"Why the third dimension is 1, not (1,2,3)?"
Part 1: Indexing
Because that is exactly what you told MATLAB to do, when you wrote your indexing, where you defined three arrays of size 2x3(x1x1x1...) like this:
A(:,:,1)
% ^ this tells MATLAB you want just ONE page from the 3rd dimension.
This applies to every dimension:
- if you pick one row via one index you will get one row (and not tree rows),
- if you pick one column via one index you will get one column (and not three columns),
- if you pick one page via one index you will get one page (and not three pages),
- etc. etc. the same applies to all dimensions of all arrays in MATLAB.
The term "page" is used in the documentation for the third dimension (just like the terms "row" used for the first dimension and "column" for the second dimension):
Part 2: Concatenation
In your example you take a 2x3x3(x1x1x1...) array and split it into three 2x3(x1x1x1...) arrays. Then you concatenate these three array along the fourth dimension, giving you a single 2x3x1x3(x1x1x1...) array. Concatenation does not change any of the other dimensions: it concatenates along the dimension of your choice, all of the other dimensions will remain exactly the same. The size of your original array is also totally irrelevant, only the sizes of the three array that you are concatenating is important.
In your example, those three arrays must have the same sizes along the other dimensions:
- 1st dim size is 2 -> CAT -> 1st dim size is 2
- 2nd dim size is 3 -> CAT -> 2nd dim size is 3
- 3rd dim size is 1 -> CAT -> 3rd dim size is 1
Do you notice the pattern: they are all unchanged. The ONLY dimension that might change (depending on the input array sizes) is the one that you are concatenating along. In your case you are concatenating three arrays whose sizes in the fourth dimension are all 1:
- 4th dim sizes are 1, 1, and 1 -> CAT -> 4th dim size is 3.
Consider this: If you concatenate two row vectors together vertically, do you expect the number of columns to change? If those two vectors are two halves of a longer vector (or rows of a matrix, or pages in an ND array, etc), does this make any difference to the concatenation? (hint: no, the concatenation operator does not know anything about where those two rows vectors came from ofr what size it might have had)
댓글 수: 9
Stephen23
2019년 4월 3일
편집: Stephen23
2019년 4월 4일
"We can see the answer delete the fifth, sixth, and seventh dimensions because the deleted dimensions are all the same."
Trailing singleton dimensions are NOT deleted. They might not be displayed, but they are (implicitly) always there, exactly as I discussed seven hours ago:
For example, you can always access them using indexing:
>> X = [2,3,5]; % size 1x3(x1x1x1x1...)
>> X(1,3,1,1,1,1,1)
ans = 5
and you can always measure them using SIZE:
>> size(X,5) % 5th dim NOT deleted!
ans = 1
>> size(X) % does not show 5th dim, but it is still there!
ans =
1 3
Which is also why I have written the sizes showing the trailing singleton dimensions, e.g.
size(B) = 2x3x1x3(x1x1x1x...)
Although sometimes MATLAB is inconsistent and has some special cases, in this case it is very consistent: trailing dimemensions do NOT get "deleted" when they are singleton.
Your concept (that singleton dimensions are somehow "deleted") would make MATLAB almost unusable, because arrays would entirely change when the data reduces down to one row/column/page/..., which would make code impossible to write without many many ugly special cases.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!