フィルターのクリア

Add a 2D matrix to a 3D matrix in a specific location

23 ビュー (過去 30 日間)
Robert
Robert 2015 年 11 月 13 日
編集済み: Stephen23 2015 年 11 月 13 日
Say I have 2 Matrices
A = 10x10x10
B = 10x10
I want to ad B to A at a certain dimension e.g. (:,:,6) such that A becomes 10x10x11
How can I do this?

回答 (2 件)

Stalin Samuel
Stalin Samuel 2015 年 11 月 13 日
l = length(A);
d = 6;%dimension where we need to insert B
New_A(:,:,1:d-1) = A(:,:,1:d-1);
New_A(:,:,d) = B;
New_A(:,:,d+1:l+1) = A(:,:,d:l);

Stephen23
Stephen23 2015 年 11 月 13 日
編集済み: Stephen23 2015 年 11 月 13 日
Method One: Indexing
>> A = randi(10,3,3,3)-1; % 3x3x3
>> B = nan(3,3); % 3x3
>> C(:,:,[1:2,4]) = A; % add A to pages [1,2,4]
>> C(:,:,3) = B % Add B to page 3
C(:,:,1) =
2 7 4
9 7 2
3 7 2
C(:,:,2) =
0 6 4
7 7 2
4 8 3
C(:,:,3) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
C(:,:,4) =
7 0 5
9 7 4
6 6 3
Method Two: cat and Indexing
>> D(:,:,[1,2,4,3]) = cat(3,A,B)
D(:,:,1) =
2 7 4
9 7 2
3 7 2
D(:,:,2) =
0 6 4
7 7 2
4 8 3
D(:,:,3) =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
D(:,:,4) =
7 0 5
9 7 4
6 6 3

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by