How to expand dimension of a 2D array

359 views (last 30 days)
banikr
banikr on 23 May 2023
Commented: John D'Errico on 24 May 2023
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.
  1 Comment
Stephen23
Stephen23 on 24 May 2023
Edited: Stephen23 on 24 May 2023
"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,999) % whever 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.3249 0.5279 0.4208 0.3191 0.7989 0.2835
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.

Sign in to comment.

Answers (3)

James Tursa
James Tursa on 23 May 2023
Edited: James Tursa on 23 May 2023
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 Comment
Steven Lord
Steven Lord on 23 May 2023
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

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 23 May 2023
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 Comments
Stephen23
Stephen23 on 24 May 2023
Edited: Stephen23 on 24 May 2023
"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 on 24 May 2023
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.

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 24 May 2023
Edited: Sulaymon Eshkabilov on 24 May 2023
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 Comment
Stephen23
Stephen23 on 24 May 2023
Edited: Stephen23 on 24 May 2023
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

Sign in to comment.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!