如何在 MATLAB 中将 2X2 矩阵转换为 4X4 矩阵?
我需要一些帮助,以以下方式将 2X2 矩阵转换为 4X4 矩阵:
A = [2 6;
8 4]
应该变成:
B = [2 2 6 6;
2 2 6 6;
8 8 4 4;
8 8 4 4]
我将如何做到这一点?
I need some help in converting a 2X2 matrix to a 4X4 matrix in the following manner:
A = [2 6;
8 4]
should become:
B = [2 2 6 6;
2 2 6 6;
8 8 4 4;
8 8 4 4]
How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在较新版本的 MATLAB(R2015a 及更高版本)中,最简单的方法是使用
repelem
函数:对于旧版本,其他(主要)基于索引的解决方案的简短替代方案是使用函数
kron
和ones
:In newer versions of MATLAB (R2015a and later) the easiest way to do this is using the
repelem
function:For older versions, a short alternative to the other (largely) indexing-based solutions is to use the functions
kron
andones
:可以比 Jason 的解决方案更容易完成:
Can be done even easier than Jason's solution:
这是另一种解决方案:
它使用索引来完成所有操作,并且不依赖于 A 的大小或形状。
Here's one more solution:
which uses indexing to do everything and doesn't rely on the size or shape of A.
这是可行的:
这只是 A(x,y) 从 x,y ∈ {1,2} 到 x,y ∈ {0.5, 1, 1.5, 2} 的二维最近邻插值。
编辑:从 Jason S 和 Martijn 的解决方案出发,我认为这可能是最短、最清晰的解决方案:
This works:
This is just two-dimensional nearest-neighbor interpolation of A(x,y) from x,y ∈ {1,2} to x,y ∈ {0.5, 1, 1.5, 2}.
Edit: Springboarding off of Jason S and Martijn's solutions, I think this is probably the shortest and clearest solution:
这是一种基于简单索引的方法,适用于任意矩阵。我们希望将每个元素扩展为 MxN 子矩阵:
示例:
要了解该方法的工作原理,让我们仔细看看索引。我们从一个简单的连续数字行向量开始
接下来,我们将其扩展为一个矩阵,通过在第一维中重复 M 次
如果我们使用矩阵来索引数组,则矩阵元素将按照标准 Matlab 顺序连续使用:
最后,在对数组进行索引时,“end”关键字计算出相应维度中数组的大小。因此,在示例中,以下内容是等效的:
Here's a method based on simple indexing that works for an arbitrary matrix. We want each element to be expanded to an MxN submatrix:
Example:
To see how the method works, let's take a closer look at the indexing. We start with a simple row vector of consecutive numbers
Next, we extend it to a matrix, by repeating it M times in the first dimension
If we use a matrix to index an array, then the matrix elements are used consecutively in the standard Matlab order:
Finally, when indexing an array, the 'end' keyword evaluates to the size of the array in the corresponding dimension. As a result, in the example the following are equivalent:
有一个 Reshape() 函数可以让您执行此操作...
例如:
您可以找到一个很棒的视频教程 这里
干杯
There is a Reshape() function that allows you to do this...
For example:
And you can find a great video tutorial here
Cheers