How to expand dimension of a 2D array

조회 수: 115 (최근 30일)
banikr
banikr 2023년 5월 23일
댓글: Steven Lord 2025년 7월 21일
I have a 2D array M with dimension 60, 60. However, I want to expand the array dimension to 60, 60, 1.
I tried by reshape which simply doesn't work for dimension 1 at the end/3rd.
M=reshape(M,[60, 1, 60]);
This changes to 60, 1, 60 but if I do:
M=reshape(M,[60, 60, 1]);
or
M=reshape(M,[size(M), 1]);
It still gives M with 60, 60.
  댓글 수: 2
Stephen23
Stephen23 2023년 5월 24일
편집: Stephen23 2025년 7월 21일
"I have a 2D array M with dimension 60, 60. However, I want to expand the array dimension to 60, 60, 1. "
There is no difference. All arrays implictly have infinite trailing singleton dimensions, so your 6x6(x1x1x1x1...) array already has size 6x6x1(x1x1x1x1...). This has been explicitly stated by TMW several times, e.g. "Arrays in MATLAB are N-dimensional, with an infinite number of trailing singleton dimensions."
Clearly there is no way for MATLAB to explicitly store the sizes of infinite singleton dimensions, nor is there any need for it to do so: you can still get the size of any dimension that you want:
M = rand(60,60);
size(M,1) % rows
ans = 60
size(M,2) % columns
ans = 60
size(M,3) % pages
ans = 1
size(M,4) % books
ans = 1
size(M,5) % shelves
ans = 1
... etc
size(M,9999) % whatever this dimension is called
ans = 1
And you can access them using indexing, if you really want to:
M(2:3,7:9,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
ans = 2×3
0.4572 0.0337 0.0507 0.2278 0.3131 0.3043
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or using multiple outputs from SIZE:
[s1,s2,s3,s4,s5,s6,s7,s8,s9] = size(M)
s1 = 60
s2 = 60
s3 = 1
s4 = 1
s5 = 1
s6 = 1
s7 = 1
s8 = 1
s9 = 1
So far you have not stated what the actual problem is.
Steven Lord
Steven Lord 2025년 7월 21일
All arrays implictly have infinite trailing singleton dimensions
If you wanted to be technical it's not infinite but it's effectively infinite. If you tried to create an array with anywhere near the theoretical upper limit of the number of dimensions you'd run into memory problems creating the array to store the size information! But you can ask for the size of an array in a surprisingly large dimension.
s = size(1:10, 2^48)
s = 1
But if you try to use too large a dimension you receive an error.
s = size(1:10, 2^48+1)
Error using size
Dimension argument must be a positive integer scalar or a vector of positive integers. When an empty dimension argument is specified, it must be a 0-by-0, 1-by-0, or 0-by-1 array.
Hmm. That error message should be updated. I thought that in the past it had mentioned something about the upper limit. Maybe it's been reworded. I'll note that to the development staff.

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

답변 (3개)

James Tursa
James Tursa 2023년 5월 23일
편집: James Tursa 2023년 5월 23일
All MATLAB trailing singleton (1) dimensions beyond the 2nd dimension are not physically stored, and will not show up in a size( ) call, but you can still treat the variable as if they are there.
A = reshape(1:9,3,3)
A = 3×3
1 4 7 2 5 8 3 6 9
size(A)
ans = 1×2
3 3
A(:,:,1) % this works, even though size( ) did not indicate the 3rd dimension existed.
ans = 3×3
1 4 7 2 5 8 3 6 9
A(:,:,1,1,1,1,1,1,1,1,1,1,1,1,1) % this also works!
ans = 3×3
1 4 7 2 5 8 3 6 9
If you do an operation (e.g., reshape) that results in singleton (1) trailing dimensions beyond the 2nd dimension, MATLAB will simply delete them from the resulting variable. There is nothing you can do to get MATLAB to physically store trailing singleton (1) dimensions beyond the 2nd dimension. But this does not affect your ability to index into these trailing dimensions using a 1 index.
And you can physically expand an array dynamically by assigning into currently non-existing indexes. E.g.,
A(4,4,2) = 10
A =
A(:,:,1) = 1 4 7 0 2 5 8 0 3 6 9 0 0 0 0 0 A(:,:,2) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10
  댓글 수: 1
Steven Lord
Steven Lord 2023년 5월 23일
Also FYI, if you explicitly ask what size your matrix is in the third dimension, MATLAB will include the size in the trailing singleton dimension.
A = magic(3)
A = 3×3
8 1 6 3 5 7 4 9 2
sz = size(A)
sz = 1×2
3 3
sz3 = size(A, 3) % or
sz3 = 1
sz123 = size(A, 1:3)
sz123 = 1×3
3 3 1

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


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 5월 23일
Yes, it is correct. It is 2D array (or matrix) but not 3D. You can create and test it with this:
M = randi(15, 60);
MM(:,:,1)=M
MM = 60×60
9 5 5 14 1 12 5 3 15 5 2 12 11 11 2 12 5 3 5 11 5 14 3 8 14 13 9 14 4 13 1 2 13 15 4 11 3 11 1 11 11 13 9 3 4 11 13 14 2 13 8 1 13 5 7 10 3 2 4 5 6 9 2 5 14 13 2 13 7 9 5 4 12 7 9 3 3 9 4 13 6 14 4 7 3 12 5 11 11 15 11 8 8 5 6 5 10 4 10 14 9 7 7 15 11 8 9 1 11 11 12 7 10 4 11 9 9 2 11 12 2 12 3 6 9 15 11 12 11 15 13 12 3 11 10 5 10 14 10 2 4 9 9 8 3 7 14 4 7 10 1 3 7 1 14 9 3 2 13 9 15 3 13 4 2 2 9 11 6 6 9 14 2 12 6 6 14 14 13 8 7 13 5 9 11 10 15 15 15 3 1 10 1 11 5 9 13 4 10 2 2 5 8 9 15 2 15 6 14 13 3 14 3 15 1 10 14 9 8 13 5 7 9 8 15 13 8 9 7 15 1 2 10 2 14 2 15 9 4 4 15 8 12 4 9 7 8 9 13 4 3 1 6 1 5 8 12 3 4 4 6 9 3 12 1 2 6 10 1 2 4 11 10 15 1 11 7 12 2 8 3 7 6 12 1 1 6 4 7 13 4 5 9 11 13 14 6 6 14 13
% MM is 60-by-60 -by-1
Now let's see how to establish a larger (3D) array
%% Mnew is 6-by-6 - 2 % To be able to view/display here
M1 = randi(5, 6);
Mnew(:,:,1)=M1
Mnew = 6×6
2 2 2 4 3 3 5 5 3 1 4 5 3 5 2 4 3 5 5 2 1 2 1 5 5 2 5 2 1 4 3 2 2 4 4 4
M2 = randi(10, 6);
Mnew(:,:,2)=M2
Mnew =
Mnew(:,:,1) = 2 2 2 4 3 3 5 5 3 1 4 5 3 5 2 4 3 5 5 2 1 2 1 5 5 2 5 2 1 4 3 2 2 4 4 4 Mnew(:,:,2) = 5 4 8 6 10 4 5 9 1 2 9 1 7 6 6 4 8 8 10 8 3 9 3 7 5 2 1 5 3 10 6 3 3 4 5 7
  댓글 수: 3
Stephen23
Stephen23 2023년 5월 24일
편집: Stephen23 2023년 5월 24일
"MM is 60-by-60 -by-1 "
So is M:
M = randi(15, 60);
[s1,s2,s3] = size(M)
s1 = 60
s2 = 60
s3 = 1
"still give 60 60 not 60 60 1. "
Which are exactly the same: both have size 60x60(x1x1x1x1x1....)
"I need to save the matrix with an additional 3rd dimension as 1."
You already made that clear in your question. Some people explained how MATLAB actually works. As the code I give in this very comment shows, your matrix does have 3rd dimension with size 1.
Repeating your request does not change how MATLAB actually works.
Rather than repreating it again, tells us what you are trying to do that does not work. As I showed, SIZE is gives you the size of any trailing dimension that you desire. I also showed that you can index into any dimension. Yet so far you have not give any explanation of what you are trying to do and why it does not work.
John D'Errico
John D'Errico 2023년 5월 24일
Think of it this way. ALL arrays, already and always have infinitely many trailing singleton dimensions.
So even though MATLAB tells you an array has size 60x60, the size is also really 60x60x1x1x1x1x1x1...
You don't really want MATLAB to display infinitely many ones when you ask for the size of a 2-d arrray? Of course not. So MATLAB just shows the leading dimensions, before the trailing singletons appear.

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


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 5월 24일
편집: Sulaymon Eshkabilov 2023년 5월 24일
M1 = randi(5, 6);
Mnew(:,:,1)=M1;
[row,col,Layer]=size(Mnew)
row = 6
col = 6
Layer = 1
%% Mnew is 6-by-6 - 2 % To be able to view/display here
M1 = randi(5, 6);
Mnew(:,:,1)=M1;
M2 = randi(10, 6);
Mnew(:,:,2)=M2;
[row,col,Layer]=size(Mnew)
row = 6
col = 6
Layer = 2
  댓글 수: 1
Stephen23
Stephen23 2023년 5월 24일
편집: Stephen23 2023년 5월 24일
The indexing here does nothing whatsoever:
Mnew(:,:,1)=M1;
because both M1 and MNEW have exactly the same size:
M1 = randi(5, 6);
[row,col,Layer] = size(M1)
row = 6
col = 6
Layer = 1

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by