Moving a small matrix inside of a bigger matrix in MATLAB
假设 A 是一个 5x5 的零矩阵:
1 2 3 4 5 6 7 8 9 |
而 B 是一个小的矩阵 (2x2):
1 2 3 4 5 6 |
现在,我正在寻找代表
矩阵的 16 种不同情况
它们是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | C1 = 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 C2 = 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 C3 = 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 |
... 最后
1 2 3 4 5 6 7 | C16 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 |
如您所见,它就像较小的矩阵(B)在较大的矩阵(A)内部移动。
非常感谢,
您可以在适当的行和列上使用
以
为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | A = [1 1 0 0; 1 1 0 0; 0 0 0 0; 0 0 0 0] A = 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 Y = circshift(A,[1 1]) Y = 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 |
在 Mathworks 网站上,有一个内置函数似乎可以完全满足您的需求。在 5x5 矩阵上显示 16 种组合的确切代码,保持小矩阵穿过大矩阵的错觉将是
已编辑:因此它现在有一个 5x5x16 矩阵,其输出称为 C
1 2 3 4 5 6 7 8 9 |
产生输出(注意输出未编辑)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | A = 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans = 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans = 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans = 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans = 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans = 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 ans = 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 ans = 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 ans = 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 ans = 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 ans = 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 ans = 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 |
的一种矢量化方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | %// Get sizes and form size parameters for creating output [mA,nA] = size(A); [mB,nB] = size(B); mC = mA - mB + 1; nC = nA - nB + 1; %// Get linear indices stage1 = bsxfun(@plus,[1:mB]',[0:nB-1]*mA); %//' stage2 = bsxfun(@plus,[1:mC]',[0:nC-1]*mA)-1; %//' idx = bsxfun(@plus,stage1(:),stage2(:).' + [0:mC*nC-1]*mA*nA); %//' %// Replicate A to setup output; index into it with idx & replace B C = repmat(A,1,1,mC*nC); C(idx) = repmat(B(:),1,mC*nC) |
样品运行 -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | A = 1 1 8 4 9 8 8 2 7 9 5 1 7 9 2 9 B = 3 5 3 6 3 1 C(:,:,1) = 3 5 8 4 3 6 8 2 3 1 5 1 7 9 2 9 C(:,:,2) = 1 1 8 4 3 5 8 2 3 6 5 1 3 1 2 9 C(:,:,3) = 1 3 5 4 9 3 6 2 7 3 1 1 7 9 2 9 .... C(:,:,6) = 1 1 8 4 9 8 3 5 7 9 3 6 7 9 3 1 |
我认为做这样的事情的好方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |