How to expand dimension of a 2D array
266 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2023 年 5 月 24 日
編集済み: Stephen23
2023 年 5 月 24 日
"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
size(M,2) % columns
size(M,3) % pages
size(M,999) % whever this dimension is called
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)
Or using multiple outputs from SIZE:
[s1,s2,s3,s4,s5,s6,s7,s8,s9] = size(M)
So far you have not stated what the actual problem is.
回答 (3 件)
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)
size(A)
A(:,:,1) % this works, even though size( ) did not indicate the 3rd dimension existed.
A(:,:,1,1,1,1,1,1,1,1,1,1,1,1,1) % this also works!
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
1 件のコメント
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)
sz = size(A)
sz3 = size(A, 3) % or
sz123 = size(A, 1:3)
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 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
M2 = randi(10, 6);
Mnew(:,:,2)=M2
3 件のコメント
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)
"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
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
2023 年 5 月 24 日
編集済み: Sulaymon Eshkabilov
2023 年 5 月 24 日
M1 = randi(5, 6);
Mnew(:,:,1)=M1;
[row,col,Layer]=size(Mnew)
%% 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)
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!