Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:具有非平方向量的块三对角_Matlab_Matrix - Fatal编程技术网

Matlab:具有非平方向量的块三对角

Matlab:具有非平方向量的块三对角,matlab,matrix,Matlab,Matrix,我试图创建一个对角线矩阵,使用下面的矩阵作为对角线 base = [a b c d e f 0; 0 g h i j k l]; 我需要得到的矩阵像这样 [a b c d e f 0 0 0; 0 g h i j k l 0 0; 0 0 a b c d e f 0; 0 0 0 g h i j k l]; 但它需要是“n”个元素的高度 我曾尝试使用kron函数,但它将每个连续的行向右移动了太多的元素 我怎样才能以一种可以任意选择n的方式完成我需要的任务?简单的方法是使

我试图创建一个对角线矩阵,使用下面的矩阵作为对角线

base = [a b c d e f 0;
        0 g h i j k l];
我需要得到的矩阵像这样

[a b c d e f 0 0 0;
 0 g h i j k l 0 0;
 0 0 a b c d e f 0;
 0 0 0 g h i j k l];
但它需要是“n”个元素的高度

我曾尝试使用
kron
函数,但它将每个连续的行向右移动了太多的元素


我怎样才能以一种可以任意选择
n
的方式完成我需要的任务?

简单的方法是使用重复越界赋值。在这些情况下,MATLAB将自动使用
0
填充任何缺失的条目。以下是方法:

%// Some test variables
a = rand;  g = rand;
b = rand;  h = rand;
c = rand;  i = rand;
d = rand;  j = rand;
e = rand;  k = rand;
f = rand;  l = rand;

%// base matrix
base = [
    a b c d e f 0;
    0 g h i j k l];

%// use out-of-bounds assignment 
n = 3;
output = base;
for ii = 1:n
    output(end+1:end+size(base,1), size(base,1)*ii+1:end+size(base,1)) = base;
end
困难的方法是更快的方法(与
n
较大和/或您需要经常重复此操作相关)。找出在最终矩阵中填充索引的模式,原始矩阵中的值由该模式填充,然后生成这些索引的列表,并将这些值分配给这些索引:

[b1,b2]      = size(base);
[ii,jj,vv]   = find(base);
inds         = bsxfun(@plus, (ii + (n+1)*b1*(jj-1)).', (0:n).'*b1*(1 + (n+1)*b1));
output       = zeros( (n+1)*b1, b2+n*b1 );
output(inds) = repmat(vv.', n+1, 1)

我将把它作为一个练习留给你去弄清楚这里到底发生了什么:)

简单的方法是使用重复的越界赋值。在这些情况下,MATLAB将自动使用
0
填充任何缺失的条目。以下是方法:

%// Some test variables
a = rand;  g = rand;
b = rand;  h = rand;
c = rand;  i = rand;
d = rand;  j = rand;
e = rand;  k = rand;
f = rand;  l = rand;

%// base matrix
base = [
    a b c d e f 0;
    0 g h i j k l];

%// use out-of-bounds assignment 
n = 3;
output = base;
for ii = 1:n
    output(end+1:end+size(base,1), size(base,1)*ii+1:end+size(base,1)) = base;
end
困难的方法是更快的方法(与
n
较大和/或您需要经常重复此操作相关)。找出在最终矩阵中填充索引的模式,原始矩阵中的值由该模式填充,然后生成这些索引的列表,并将这些值分配给这些索引:

[b1,b2]      = size(base);
[ii,jj,vv]   = find(base);
inds         = bsxfun(@plus, (ii + (n+1)*b1*(jj-1)).', (0:n).'*b1*(1 + (n+1)*b1));
output       = zeros( (n+1)*b1, b2+n*b1 );
output(inds) = repmat(vv.', n+1, 1)

我将把它作为一个练习留给你去弄清楚这里到底发生了什么:)

简单的方法是使用重复的越界赋值。在这些情况下,MATLAB将自动使用
0
填充任何缺失的条目。以下是方法:

%// Some test variables
a = rand;  g = rand;
b = rand;  h = rand;
c = rand;  i = rand;
d = rand;  j = rand;
e = rand;  k = rand;
f = rand;  l = rand;

%// base matrix
base = [
    a b c d e f 0;
    0 g h i j k l];

%// use out-of-bounds assignment 
n = 3;
output = base;
for ii = 1:n
    output(end+1:end+size(base,1), size(base,1)*ii+1:end+size(base,1)) = base;
end
困难的方法是更快的方法(与
n
较大和/或您需要经常重复此操作相关)。找出在最终矩阵中填充索引的模式,原始矩阵中的值由该模式填充,然后生成这些索引的列表,并将这些值分配给这些索引:

[b1,b2]      = size(base);
[ii,jj,vv]   = find(base);
inds         = bsxfun(@plus, (ii + (n+1)*b1*(jj-1)).', (0:n).'*b1*(1 + (n+1)*b1));
output       = zeros( (n+1)*b1, b2+n*b1 );
output(inds) = repmat(vv.', n+1, 1)

我将把它作为一个练习留给你去弄清楚这里到底发生了什么:)

简单的方法是使用重复的越界赋值。在这些情况下,MATLAB将自动使用
0
填充任何缺失的条目。以下是方法:

%// Some test variables
a = rand;  g = rand;
b = rand;  h = rand;
c = rand;  i = rand;
d = rand;  j = rand;
e = rand;  k = rand;
f = rand;  l = rand;

%// base matrix
base = [
    a b c d e f 0;
    0 g h i j k l];

%// use out-of-bounds assignment 
n = 3;
output = base;
for ii = 1:n
    output(end+1:end+size(base,1), size(base,1)*ii+1:end+size(base,1)) = base;
end
困难的方法是更快的方法(与
n
较大和/或您需要经常重复此操作相关)。找出在最终矩阵中填充索引的模式,原始矩阵中的值由该模式填充,然后生成这些索引的列表,并将这些值分配给这些索引:

[b1,b2]      = size(base);
[ii,jj,vv]   = find(base);
inds         = bsxfun(@plus, (ii + (n+1)*b1*(jj-1)).', (0:n).'*b1*(1 + (n+1)*b1));
output       = zeros( (n+1)*b1, b2+n*b1 );
output(inds) = repmat(vv.', n+1, 1)

我将把它作为一个练习留给你,让你弄清楚这里到底发生了什么:)

你可以用二维卷积非常快地完成它:

n = 4; %// desired number of rows in result. Should be a multiple of size(base,1)
T = eye(n-1);
T(2:size(base,1):end,:) = 0;
result = conv2(base,T);
示例:与

base =

    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0
         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578
并且
n=4
结果是

result =

    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0         0         0
         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578         0         0
         0         0    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0
         0         0         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578

使用2D卷积可以非常快速地完成:

n = 4; %// desired number of rows in result. Should be a multiple of size(base,1)
T = eye(n-1);
T(2:size(base,1):end,:) = 0;
result = conv2(base,T);
示例:与

base =

    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0
         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578
并且
n=4
结果是

result =

    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0         0         0
         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578         0         0
         0         0    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0
         0         0         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578

使用2D卷积可以非常快速地完成:

n = 4; %// desired number of rows in result. Should be a multiple of size(base,1)
T = eye(n-1);
T(2:size(base,1):end,:) = 0;
result = conv2(base,T);
示例:与

base =

    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0
         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578
并且
n=4
结果是

result =

    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0         0         0
         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578         0         0
         0         0    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0
         0         0         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578

使用2D卷积可以非常快速地完成:

n = 4; %// desired number of rows in result. Should be a multiple of size(base,1)
T = eye(n-1);
T(2:size(base,1):end,:) = 0;
result = conv2(base,T);
示例:与

base =

    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0
         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578
并且
n=4
结果是

result =

    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0         0         0
         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578         0         0
         0         0    0.7497    0.3782    0.4470    0.5118    0.6698    0.3329         0
         0         0         0    0.9850    0.5638    0.9895    0.4362    0.4545    0.8578


n
是否总是偶数?或者你认为你所说的矩阵是“代码>2”/代码>高吗?为什么有这么多的MATLAB作业问题,所以没有尝试的代码?显示你尝试过的代码> KRON/<代码>是<代码> N< /代码>总是?或者你认为你所说的矩阵是“代码>2”/代码>高吗?为什么有这么多的MATLAB作业问题,所以没有尝试的代码?显示你尝试过的代码> KRON/<代码>是<代码> N< /代码>总是?或者你认为你所说的矩阵是“代码>2”/代码>高吗?为什么有这么多的MATLAB作业问题,所以没有尝试的代码?显示你尝试过的代码> KRON/<代码>是<代码> N< /代码>总是?或者你认为你显示的矩阵是“代码>2”/代码>高吗?为什么有这么多的MATLAB作业问题,所以没有尝试的代码?显示你尝试过的<代码> KRON < /代码> + 1:好!尽管我的解决方案对于大型
n
,似乎扩展得更好,
conv2
似乎是O(n²),或者至少是超线性的……这是真的吗?@RodyOldenhuis谢谢!是的,我敢打赌它至少是超线性的。但是我还没有测试+1:很好!尽管我的解决方案对于大型
n
,似乎扩展得更好,
conv2
似乎是O(n²),或者至少是超线性的……这是真的吗?@RodyOldenhuis谢谢!是的,我敢打赌它至少是超线性的。但是我还没有测试+1:很好!尽管我的解决方案对于大型
n
,似乎扩展得更好,
conv2
似乎是O(n²),或者至少是超线性的……这是真的吗?@RodyOldenhuis谢谢!是的,我敢打赌它至少是超线性的。但是我还没有测试+1:很好!尽管我的解决方案对于大型
n
,似乎扩展得更好,
conv2
似乎是O(n²),或者至少是超线性的……这是真的吗?@RodyOldenhuis谢谢!是的,我敢打赌它至少是超线性的。但我还没有测试