Adding matrices to a 3D matrix in a specific order
1 view (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Stephen23
on 3 Nov 2015
Edited: Stephen23
on 3 Nov 2015
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:
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!