Fill up submatrix along third dimension with two dimensional matrix

조회 수: 2 (최근 30일)
Given a p by p by N array, e.g. p=3 and N=4,
x = NaN(3,3,4);
I can easily fill up the 1 by 1 by N submatrix given a "1 by 1 matrix"doing
x(1,1,:) = 1;
But what if I want to fill up the 2 by 2 by N submatrix like this
x(1:2,1:2,:) = [1 2; 3 4];
This does not work.
How could I do it elegantly?

채택된 답변

Bruno Luong
Bruno Luong 2022년 8월 29일
편집: Bruno Luong 2022년 8월 29일
x(1:2,1:2,:) = repmat( [1 2; 3 4], 1, 1, size(x,3));
I would love to have MATLAB indexing to behave with auto expansion, but probably many users would be unhappy and complain because of coding error becoming silence.

추가 답변 (1개)

Torsten
Torsten 2022년 8월 29일
x = NaN(3,3,4);
x(1:2,1:2,:) = repmat([1 2; 3 4],1,1,4);
x
x =
x(:,:,1) = 1 2 NaN 3 4 NaN NaN NaN NaN x(:,:,2) = 1 2 NaN 3 4 NaN NaN NaN NaN x(:,:,3) = 1 2 NaN 3 4 NaN NaN NaN NaN x(:,:,4) = 1 2 NaN 3 4 NaN NaN NaN NaN

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by