Adding matrices to a 3D matrix in a specific order
조회 수: 2 (최근 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?
댓글 수: 0
답변 (1개)
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:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!