フィルターのクリア

Adding matrices to a 3D matrix in a specific order

1 回表示 (過去 30 日間)
Robert
Robert 2015 年 11 月 3 日
編集済み: Stephen23 2015 年 11 月 3 日
Say I have a 3D matrix 50x50x100
Now I want to add another matrix to make it 50x50x101, however I want the new matrix to go in a particular position e.g. the 10th out of the 101 matrices.
How can I achieve this?

回答 (1 件)

Stephen23
Stephen23 2015 年 11 月 3 日
編集済み: Stephen23 2015 年 11 月 3 日
This is easy using MATLAB indexing. Here is an example with 2D matrices, to show how indexing can be used to insert values in particular locations of a new matrix:
>> M = [1,2,3;4,5,6] % original matrix
M =
1 2 3
4 5 6
>> N(:,[1,3:4]) = M % insert M into cols 1,3 and 4 of N
N =
1 0 2 3
4 0 5 6
>> N(:,2) = [Inf,NaN] % insert into col 2 of N
N =
1 Inf 2 3
4 NaN 5 6
And here is a 3D example as well:
>> X = reshape([1,2,3,4,5,6],1,2,3); % original array
>> Y(:,:,[1,3:4]) = X; % insert into pages 1, 3 and 4 of Y
>> Y(:,:,2) = [Inf,NaN] % insert into page 2 of Y
Y(:,:,1) =
1 2
Y(:,:,2) =
Inf NaN
Y(:,:,3) =
3 4
Y(:,:,4) =
5 6
More information on indexing:

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by