I substracted two 3D matrix and get a 2D matrix instead of 3D matrix, why?

5 ビュー (過去 30 日間)
Wang Ryan
Wang Ryan 2024 年 9 月 5 日
コメント済み: DGM 2024 年 9 月 6 日
I have a 3264x4912x3 matrix p, I subtracted p by the following code:
X=4912; Y=3264;d=1; dp=minus(p(1:Y,1:X-d),p(1:Y,1+d:X));
Then I get 3264x4911 matrix dp. Could anyone tell me why can't I get a 3d matrix (3264x4911x3) by subtracting two 3D matrix? Many thanks.

回答 (2 件)

Bruno Luong
Bruno Luong 2024 年 9 月 5 日
編集済み: Bruno Luong 2024 年 9 月 5 日
You need to put 3 indexes in 3d array. If you put 2 it reshape your array to 2D.
The correct command is probably
dp=minus(p(1:Y,1:X-d,:),p(1:Y,1+d:X,:))
  1 件のコメント
Bruno Luong
Bruno Luong 2024 年 9 月 5 日
編集済み: Bruno Luong 2024 年 9 月 6 日
To facilitate the understanding of indexing behavior with number of index less than the dimension of original matrix, in other word how MATLAB reshape implicitly, try this examples
A = rand(2,3,5);
size(A)
ans = 1x3
2 3 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size(A(:,:,:))
ans = 1x3
2 3 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size(A(:,:))
ans = 1x2
2 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size(A(:))
ans = 1x2
30 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size(A(:,:,:,:))
ans = 1x3
2 3 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size(A(:,:,:,:,:)) % yes all trailing dimensions are 1s but not displayed
ans = 1x3
2 3 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

サインインしてコメントする。


Vinay
Vinay 2024 年 9 月 5 日
Hii Wang,
The original matrix p has dimensions 3264x4912x3. When using p(1:Y, 1:X-d) and p(1:Y, 1+d:X), you are slicing the first two dimensions and ignoring the third dimension.
Therefore the result of the subtraction is a 2D matrix of size 3264x4911
ans = p(1:Y, 1:X-d)- p(1:Y, 1+d:X)
The result can be obtained as 3D matrix by using the below code.
result = zeros(Y, X-d, 3);
result = p(1:Y, 1:X-d, :) - p(1:Y, 1+d:X, :);
  2 件のコメント
Stephen23
Stephen23 2024 年 9 月 5 日
編集済み: Stephen23 2024 年 9 月 5 日
"you are slicing the first two dimensions and ignoring the third dimension."
No, that is incorrect.
In fact all trailing dimensions collapse into the last subscript index. As Loren Shure wrote: "Indexing with fewer indices than dimensions If the final dimension i<N, the right-hand dimensions collapse into the final dimension."
If you think about it, linear indexing is really just a side-effect of this. Other discussions on this topic:
DGM
DGM 2024 年 9 月 6 日
Similarly, this helps explain why size() behaves the way it does when using an underspecified set of scalar outputs:
A = rand(2,3,5);
sz1 = size(A(:,:,:))
sz1 = 1x3
2 3 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[d1,d2,d3] = size(A)
d1 = 2
d2 = 3
d3 = 5
sz2 = size(A(:,:)) % the input to size() is reshaped first
sz2 = 1x2
2 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[d1,d2] = size(A) % the input to size() is not reshaped
d1 = 2
d2 = 15

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by