用Matlab对角扩展矩阵

用Matlab对角扩展矩阵

本文介绍了用Matlab对角扩展矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵 B ,我想通过获得尺寸为(L + k)* m 的矩阵 C L * n . L k 是输入值. B0,B1,...,Bk 的大小为 m ,大小为 n .

I have a matrix B and I want to obtain a matrix C of dimension (L+k)*m by L*n. L and k are input values. B0 , B1 , ... , Bk has size m by n.

例如:

如果我有一个矩阵 B = [1 1;1 1;1 1] ,其中 B0 = [1 1] B1 = [1 1] B2 = [1 1] ,以及每个尺寸为 1 乘以 2 B0,B1,B2 ,其中 k = 2 L =4 .

If I have a matrix B = [1 1 ; 1 1 ; 1 1] with B0 = [1 1], B1 = [1 1] and B2 = [1 1], and each B0 , B1 , B2 of dimension 1 by 2 with k = 2 and L = 4.

则获得的矩阵 C C = [1 1 0 0 0 0 0 0;1 1 1 1 0 0 0 0;1 1 1 1 1 1 0 0 00 0 1 1 1 1 1 1;0 0 0 0 1 1 1 1;0 0 0 0 0 0 1 1] 和尺寸 6 通过 8 .

Then the matrix C obtained is given by C = [1 1 0 0 0 0 0 0 ; 1 1 1 1 0 0 0 0 ; 1 1 1 1 1 1 0 0 ; 0 0 1 1 1 1 1 1 ; 0 0 0 0 1 1 1 1 ; 0 0 0 0 0 0 1 1] and of dimension6 by 8.

我想针对任何大小的矩阵 B 概括我的程序.

I would like to generalize my program for any size matrix B.

我的程序解决了 B = [1 1;1 1;1 1] ,其中 m = 1 n = 2 k = 2 L = 4 .

My program solves the problem for B = [1 1 ; 1 1 ; 1 1] with m = 1 , n = 2 , k = 2 and L = 4.

我的代码:

clc;
clear;

k = 2;
L = 4;

B = [1 1 ; 1 1 ; 1 1];
B0 = [1 1];
B1 = [1 1];
B2 = [1 1];
m = size(B0,1);
n = size(B0,2);

c = [B ; zeros(size(B))];
C = zeros((L+k)*m,L*n);

for i = 1:L

    C(:,2*i-1:2*i) = circshift(c,i-1,1);

end

结果: C =

1   1   0   0   0   0   0   0
1   1   1   1   0   0   0   0
1   1   1   1   1   1   0   0
0   0   1   1   1   1   1   1
0   0   0   0   1   1   1   1
0   0   0   0   0   0   1   1

对于任何给定的矩阵 B k L 的任何值,我都难以归纳.

I have difficulties to generalize for any given matrix B and for any value of k and L.

有什么建议吗?

推荐答案

B = [1 2 3; 4 5 6; 7 8 9; 100 110 120; 130 140 150; 160 170 180];  % input
L = 4;                                                             % input
k = 2;                                                             % input
m = size(B,1)/(k+1);              % obtain m
n = size(B,2);                    % obtain n
C = zeros((L-1)*m+1, (L-1)*n+1);  % initiallize result
C(1:((L-1)*m+1)*n+m:end) = 1;     % each 1 marks the upper-left corner for a copy of B
C = conv2(C, B);                  % insert copies of B, extending size

倒数第二行使用 linear索引.最后一行适用二维卷积.在此示例中的结果是

The second-to-last line uses linear indexing. The last line applies two-dimensional convolution. The result in this example is

B =
     1     2     3
     4     5     6
     7     8     9
   100   110   120
   130   140   150
   160   170   180

C =
     1     2     3     0     0     0     0     0     0     0     0     0
     4     5     6     0     0     0     0     0     0     0     0     0
     7     8     9     1     2     3     0     0     0     0     0     0
   100   110   120     4     5     6     0     0     0     0     0     0
   130   140   150     7     8     9     1     2     3     0     0     0
   160   170   180   100   110   120     4     5     6     0     0     0
     0     0     0   130   140   150     7     8     9     1     2     3
     0     0     0   160   170   180   100   110   120     4     5     6
     0     0     0     0     0     0   130   140   150     7     8     9
     0     0     0     0     0     0   160   170   180   100   110   120
     0     0     0     0     0     0     0     0     0   130   140   150
     0     0     0     0     0     0     0     0     0   160   170   180

这篇关于用Matlab对角扩展矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:16