Adding matrices to a 3D matrix in a specific order

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by