如何在 MATLAB 中将 2X2 矩阵转换为 4X4 矩阵?

发布于 2024-08-09 12:06:54 字数 200 浏览 13 评论 0原文

我需要一些帮助,以以下方式将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

岛歌少女 2024-08-16 12:06:54

在较新版本的 MATLAB(R2015a 及更高版本)中,最简单的方法是使用 repelem 函数:

B = repelem(A, 2, 2);

对于旧版本,其他(主要)基于索引的解决方案的简短替代方案是使用函数 kronones

>> A = [2 6; 8 4];
>> B = kron(A, ones(2))

B =

     2     2     6     6
     2     2     6     6
     8     8     4     4
     8     8     4     4

In newer versions of MATLAB (R2015a and later) the easiest way to do this is using the repelem function:

B = repelem(A, 2, 2);

For older versions, a short alternative to the other (largely) indexing-based solutions is to use the functions kron and ones:

>> A = [2 6; 8 4];
>> B = kron(A, ones(2))

B =

     2     2     6     6
     2     2     6     6
     8     8     4     4
     8     8     4     4
最笨的告白 2024-08-16 12:06:54

可以比 Jason 的解决方案更容易完成:

B = A([1 1 2 2], :);  % replicate the rows
B = B(:, [1 1 2 2]);  % replicate the columns

Can be done even easier than Jason's solution:

B = A([1 1 2 2], :);  % replicate the rows
B = B(:, [1 1 2 2]);  % replicate the columns
御守 2024-08-16 12:06:54

这是另一种解决方案:

A = [2 6; 8 4];
B = A( ceil( 0.5:0.5:end ), ceil( 0.5:0.5:end ) );

它使用索引来完成所有操作,并且不依赖于 A 的大小或形状。

Here's one more solution:

A = [2 6; 8 4];
B = A( ceil( 0.5:0.5:end ), ceil( 0.5:0.5:end ) );

which uses indexing to do everything and doesn't rely on the size or shape of A.

人事已非 2024-08-16 12:06:54

这是可行的:

A = [2 6; 8 4];
[X,Y] = meshgrid(1:2);
[XI,YI] = meshgrid(0.5:0.5:2);
B = interp2(X,Y,A,XI,YI,'nearest');

这只是 A(x,y) 从 x,y ∈ {1,2} 到 x,y ∈ {0.5, 1, 1.5, 2} 的二维最近邻插值。

编辑:从 Jason S 和 Martijn 的解决方案出发,我认为这可能是最短、最清晰的解决方案:

A = [2 6; 8 4];
B = A([1 1 2 2], [1 1 2 2]);

This works:

A = [2 6; 8 4];
[X,Y] = meshgrid(1:2);
[XI,YI] = meshgrid(0.5:0.5:2);
B = interp2(X,Y,A,XI,YI,'nearest');

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:

A = [2 6; 8 4];
B = A([1 1 2 2], [1 1 2 2]);
毁梦 2024-08-16 12:06:54
A = [2 6; 8 4];
% arbitrary 2x2 input matrix

B = repmat(A,2,2);
% replicates rows & columns but not in the way you want

B = B([1 3 2 4], :);
% swaps rows 2 and 3

B = B(:, [1 3 2 4]);
% swaps columns 2 and 3, and you're done!
A = [2 6; 8 4];
% arbitrary 2x2 input matrix

B = repmat(A,2,2);
% replicates rows & columns but not in the way you want

B = B([1 3 2 4], :);
% swaps rows 2 and 3

B = B(:, [1 3 2 4]);
% swaps columns 2 and 3, and you're done!
耳根太软 2024-08-16 12:06:54

这是一种基于简单索引的方法,适用于任意矩阵。我们希望将每个元素扩展为 MxN 子矩阵:

A(repmat(1:end,[M 1]),repmat(1:end,[N 1]))

示例:

>> A=reshape(1:6,[2,3])

A =

     1     3     5
     2     4     6

>> A(repmat(1:end,[3 1]),repmat(1:end,[4 1]))

ans =

     1     1     1     1     3     3     3     3     5     5     5     5
     1     1     1     1     3     3     3     3     5     5     5     5
     1     1     1     1     3     3     3     3     5     5     5     5
     2     2     2     2     4     4     4     4     6     6     6     6
     2     2     2     2     4     4     4     4     6     6     6     6
     2     2     2     2     4     4     4     4     6     6     6     6

要了解该方法的工作原理,让我们仔细看看索引。我们从一个简单的连续数字行向量开始

>> m=3; 1:m

ans =

     1     2     3

接下来,我们将其扩展为一个矩阵,通过在第一维中重复 M 次

>> M=4; I=repmat(1:m,[M 1])

I =

     1     2     3
     1     2     3
     1     2     3
     1     2     3

如果我们使用矩阵来索引数组,则矩阵元素将按照标准 Matlab 顺序连续使用:

>> I(:)

ans =

     1
     1
     1
     1
     2
     2
     2
     2
     3
     3
     3
     3

最后,在对数组进行索引时,“end”关键字计算出相应维度中数组的大小。因此,在示例中,以下内容是等效的:

>> A(repmat(1:end,[3 1]),repmat(1:end,[4 1]))
>> A(repmat(1:2,[3 1]),repmat(1:3,[4 1]))
>> A(repmat([1 2],[3 1]),repmat([1 2 3],[4 1]))
>> A([1 2;1 2;1 2],[1 2 3;1 2 3;1 2 3;1 2 3])
>> A([1 1 1 2 2 2],[1 1 1 1 2 2 2 2 3 3 3 3])

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:

A(repmat(1:end,[M 1]),repmat(1:end,[N 1]))

Example:

>> A=reshape(1:6,[2,3])

A =

     1     3     5
     2     4     6

>> A(repmat(1:end,[3 1]),repmat(1:end,[4 1]))

ans =

     1     1     1     1     3     3     3     3     5     5     5     5
     1     1     1     1     3     3     3     3     5     5     5     5
     1     1     1     1     3     3     3     3     5     5     5     5
     2     2     2     2     4     4     4     4     6     6     6     6
     2     2     2     2     4     4     4     4     6     6     6     6
     2     2     2     2     4     4     4     4     6     6     6     6

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

>> m=3; 1:m

ans =

     1     2     3

Next, we extend it to a matrix, by repeating it M times in the first dimension

>> M=4; I=repmat(1:m,[M 1])

I =

     1     2     3
     1     2     3
     1     2     3
     1     2     3

If we use a matrix to index an array, then the matrix elements are used consecutively in the standard Matlab order:

>> I(:)

ans =

     1
     1
     1
     1
     2
     2
     2
     2
     3
     3
     3
     3

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:

>> A(repmat(1:end,[3 1]),repmat(1:end,[4 1]))
>> A(repmat(1:2,[3 1]),repmat(1:3,[4 1]))
>> A(repmat([1 2],[3 1]),repmat([1 2 3],[4 1]))
>> A([1 2;1 2;1 2],[1 2 3;1 2 3;1 2 3;1 2 3])
>> A([1 1 1 2 2 2],[1 1 1 1 2 2 2 2 3 3 3 3])
后eg是否自 2024-08-16 12:06:54

有一个 Reshape() 函数可以让您执行此操作...

例如:

reshape(array, [64, 16])

您可以找到一个很棒的视频教程 这里

干杯

There is a Reshape() function that allows you to do this...

For example:

reshape(array, [64, 16])

And you can find a great video tutorial here

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文