Insert Zeros in every n-rows at the regular interval in 3rd dimension for an existing 3D Matrix.

2 次查看(过去 30 天)
I have a m*n*t matrix. In 3rd dimension t, I want to insert m*n zeros at a particular interval. For example input
A(:,:,1) =
9 8 -8 -3
-3 -3 7 8
-3 -10 -1 8
10 5 -10 -10
4 7 11 13
A(:,:,2) =
14 9 7 5
3 5 2 11
15 12 15 8
7 10 9 5
14 4 15 13
A(:,:,3) =
14 17 17 15
13 21 21 19
15 21 17 17
14 21 18 13
17 16 13 17
A(:,:,4) =
14 21 15 19
15 12 20 11
15 5 4 9
7 6 15 16
3 2 17 10
result would be:
A(:,:,1) =
9 8 -8 -3
-3 -3 7 8
-3 -10 -1 8
10 5 -10 -10
4 7 11 13
A(:,:,2) =
14 9 7 5
3 5 2 11
15 12 15 8
7 10 9 5
14 4 15 13
A(:,:,3) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
A(:,:,4) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
A(:,:,5) =
14 17 17 15
13 21 21 19
15 21 17 17
14 21 18 13
17 16 13 17
A(:,:,6) =
14 21 15 19
15 12 20 11
15 5 4 9
7 6 15 16
3 2 17 10
A(:,:,7) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
A(:,:,8) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Thanks!

采纳的回答

Matt J
Matt J 2018-4-24
编辑:Matt J 2018-4-24
Assuming every page really is m x n (cf. Stephen's comment),
A=rand(3,4,6); %fake data
pattern=[1 1 1 0 0]; %insert zero every 4th and 5th page
[m,n,t]=size(A);
q=floor(t/nnz(pattern));
idx=repmat( reshape(pattern,1,1,[]) , 1,1,q)& true(m,n);
out=zeros(size(idx));
out(idx)=A(1:nnz(idx)),
results in
out(:,:,1) =
0.8147 0.9134 0.2785 0.9649
0.9058 0.6324 0.5469 0.1576
0.1270 0.0975 0.9575 0.9706
out(:,:,2) =
0.9572 0.1419 0.7922 0.0357
0.4854 0.4218 0.9595 0.8491
0.8003 0.9157 0.6557 0.9340
out(:,:,3) =
0.6787 0.3922 0.7060 0.0462
0.7577 0.6555 0.0318 0.0971
0.7431 0.1712 0.2769 0.8235
out(:,:,4) =
0 0 0 0
0 0 0 0
0 0 0 0
out(:,:,5) =
0 0 0 0
0 0 0 0
0 0 0 0
out(:,:,6) =
0.6948 0.0344 0.7655 0.4898
0.3171 0.4387 0.7952 0.4456
0.9502 0.3816 0.1869 0.6463
out(:,:,7) =
0.7094 0.6797 0.1190 0.3404
0.7547 0.6551 0.4984 0.5853
0.2760 0.1626 0.9597 0.2238
out(:,:,8) =
0.7513 0.6991 0.5472 0.2575
0.2551 0.8909 0.1386 0.8407
0.5060 0.9593 0.1493 0.2543
out(:,:,9) =
0 0 0 0
0 0 0 0
0 0 0 0
out(:,:,10) =
0 0 0 0
0 0 0 0
0 0 0 0

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by