Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.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 - Fatal编程技术网

Matlab:按列交错采样

Matlab:按列交错采样,matlab,Matlab,在MatLab中,我有两个矩阵(矩阵的大小可以不同)。我想从B中获取列,并在A中每隔n列插入它们 下面是一个小例子: 案例一 本案例的假设: 列是2的任意倍数,行对于A和B是相同的 A= B= 我想要这个 c= 代码还应适用于不同的情况,例如: 案例二 本案例的假设: 列是3的任意倍数,行对于A和B是相同的 A= B= C= 我知道MatLab有一个整形和置换函数,但我一直无法得到正确的结果。我知道我可以使用for循环,或者通过索引手动对它们进行处理,但是我认为使用这些函数会有更好的性能。任何h

在MatLab中,我有两个矩阵(矩阵的大小可以不同)。我想从B中获取列,并在A中每隔n列插入它们

下面是一个小例子:

案例一

本案例的假设:

列是2的任意倍数,行对于A和B是相同的

A=

B=

我想要这个

c=

代码还应适用于不同的情况,例如:

案例二

本案例的假设:

列是3的任意倍数,行对于A和B是相同的

A=

B=

C=


我知道MatLab有一个整形和置换函数,但我一直无法得到正确的结果。我知道我可以使用for循环,或者通过索引手动对它们进行处理,但是我认为使用这些函数会有更好的性能。任何hep都很好。

我想我解决了这个难题:

  • 我将A和B矩阵转换为向量,因为向量索引比矩阵索引更容易使用
  • 我使用索引模维数准备了a和B元素的正确索引向量
检查以下解决方案:

A = [1 2 3 4
     5 6 7 8];

B =[1 2
    3 4];

aN = size(A, 2);
bN = size(B, 2);

cM = size(A,1);
cN = aN + bN;

a = A(:);
b = B(:);

%Initialize vector C with zeros.
c = zeros(length(a) + length(b), 1);

a_indeces = 1:length(c);
b_indeces = 1:length(c);

%Create array of indeces matches rquiered position of A elements.
a_indeces = a_indeces(mod(a_indeces-1, cN) < aN); %[1:4, 7:10]

%Create array of indeces matches rquiered position of B elements (add aN because B elements follows A elements).
b_indeces = b_indeces(mod(b_indeces-1, cN) < bN) + aN; %[5:6, 11:12]

%Fill c elements as vector.
c(a_indeces) = a;
c(b_indeces) = b;

%Reshape c to matrix.
C = reshape(c, [cM, cN]);
A=[1 2 3 4
5 6 7 8];
B=[1 2
3 4];
aN=尺寸(A,2);
bN=尺寸(B,2);
cM=尺寸(A,1);
cN=aN+bN;
a=a(:);
b=b(:);
%用零初始化向量C。
c=零(长度(a)+长度(b),1);
a_指数=1:长度(c);
b_指数=1:长度(c);
%创建索引数组以匹配元素的静态位置。
a_指数=a_指数(mod(a_指数-1,cN)

我发现更优雅的方式,效果更好:

A = [ 1   2  3  4
      5   6  7  8
      9  10 11 12];

B =[11 12
    13 14
    15 16];

aN = size(A, 2);
bN = size(B, 2);
cN = aN + bN;

a_indeces = 1:cN;
b_indeces = 1:cN;

%Create array of column indeces matches A elements.
a_indeces = a_indeces(mod(a_indeces-1, floor(cN/bN)) < floor(aN/bN));

%Create array of column indeces matches B elements.
b_indeces = b_indeces(mod(b_indeces-1, floor(cN/bN)) >= floor(aN/bN));

C = zeros(size(A,1), cN);

C(:, a_indeces) = A;
C(:, b_indeces) = B;
A=[1 2 3 4
5   6  7  8
9  10 11 12];
B=[11 12
13 14
15 16];
aN=尺寸(A,2);
bN=尺寸(B,2);
cN=aN+bN;
a_指数=1:cN;
b_指数=1:cN;
%创建与元素匹配的列索引数组。
a_指数=a_指数(mod(a_指数-1,下限(cN/bN))<下限(aN/bN));
%创建与B元素匹配的列索引数组。
b_指数=b_指数(mod(b_指数-1,楼层(cN/bN))>=楼层(aN/bN));
C=零(尺寸(A,1),cN);
C(:,a_indes)=a;
C(:,b_指数)=b;

我想我解决了这个难题:

  • 我将A和B矩阵转换为向量,因为向量索引比矩阵索引更容易使用
  • 我使用索引模维数准备了a和B元素的正确索引向量
检查以下解决方案:

A = [1 2 3 4
     5 6 7 8];

B =[1 2
    3 4];

aN = size(A, 2);
bN = size(B, 2);

cM = size(A,1);
cN = aN + bN;

a = A(:);
b = B(:);

%Initialize vector C with zeros.
c = zeros(length(a) + length(b), 1);

a_indeces = 1:length(c);
b_indeces = 1:length(c);

%Create array of indeces matches rquiered position of A elements.
a_indeces = a_indeces(mod(a_indeces-1, cN) < aN); %[1:4, 7:10]

%Create array of indeces matches rquiered position of B elements (add aN because B elements follows A elements).
b_indeces = b_indeces(mod(b_indeces-1, cN) < bN) + aN; %[5:6, 11:12]

%Fill c elements as vector.
c(a_indeces) = a;
c(b_indeces) = b;

%Reshape c to matrix.
C = reshape(c, [cM, cN]);
A=[1 2 3 4
5 6 7 8];
B=[1 2
3 4];
aN=尺寸(A,2);
bN=尺寸(B,2);
cM=尺寸(A,1);
cN=aN+bN;
a=a(:);
b=b(:);
%用零初始化向量C。
c=零(长度(a)+长度(b),1);
a_指数=1:长度(c);
b_指数=1:长度(c);
%创建索引数组以匹配元素的静态位置。
a_指数=a_指数(mod(a_指数-1,cN)

我发现更优雅的方式,效果更好:

A = [ 1   2  3  4
      5   6  7  8
      9  10 11 12];

B =[11 12
    13 14
    15 16];

aN = size(A, 2);
bN = size(B, 2);
cN = aN + bN;

a_indeces = 1:cN;
b_indeces = 1:cN;

%Create array of column indeces matches A elements.
a_indeces = a_indeces(mod(a_indeces-1, floor(cN/bN)) < floor(aN/bN));

%Create array of column indeces matches B elements.
b_indeces = b_indeces(mod(b_indeces-1, floor(cN/bN)) >= floor(aN/bN));

C = zeros(size(A,1), cN);

C(:, a_indeces) = A;
C(:, b_indeces) = B;
A=[1 2 3 4
5   6  7  8
9  10 11 12];
B=[11 12
13 14
15 16];
aN=尺寸(A,2);
bN=尺寸(B,2);
cN=aN+bN;
a_指数=1:cN;
b_指数=1:cN;
%创建与元素匹配的列索引数组。
a_指数=a_指数(mod(a_指数-1,下限(cN/bN))<下限(aN/bN));
%创建与B元素匹配的列索引数组。
b_指数=b_指数(mod(b_指数-1,楼层(cN/bN))>=楼层(aN/bN));
C=零(尺寸(A,1),cN);
C(:,a_indes)=a;
C(:,b_指数)=b;

假设A的宽度总是B的n倍,可以使用以下代码生成c:

 A = [ 1 2 3 4 5 6; 7 8 9 10 11 12 ]
 B = [ 13 14; 15 16 ]

 NA = size(A, 2);
 NB = size(B, 2);
 n = NA / NB;

 # Make a combined array
 AB = [A B]

 # Pick columns from A and B part in AB, respectively
 colsA = reshape(1:NA, [n, NB])
 colsB = reshape(1:NB, [1, NB]) + NA

 cols = [colsA; colsB] 


 c = AB(:, cols(:))

基本思想是首先将A和B水平连接为AB。然后c将等于AB(:,[1:n,NA+1,(1:n)+n,NA+2,(1:n)+2*n,NA+3…])
列索引数组是通过交错
[1:n(1:n)+n,(1:n)+2*n…]
[NA+1,NA+2,NA+3…]
生成的;两者都可以像上面那样使用重塑来方便地构造。

假设A总是正好是B的n倍宽,您可以使用以下代码来生成c:

 A = [ 1 2 3 4 5 6; 7 8 9 10 11 12 ]
 B = [ 13 14; 15 16 ]

 NA = size(A, 2);
 NB = size(B, 2);
 n = NA / NB;

 # Make a combined array
 AB = [A B]

 # Pick columns from A and B part in AB, respectively
 colsA = reshape(1:NA, [n, NB])
 colsB = reshape(1:NB, [1, NB]) + NA

 cols = [colsA; colsB] 


 c = AB(:, cols(:))

基本思想是首先将A和B水平连接为AB。然后c将等于AB(:,[1:n,NA+1,(1:n)+n,NA+2,(1:n)+2*n,NA+3…])
列索引数组是通过交错
[1:n(1:n)+n,(1:n)+2*n…]
[NA+1,NA+2,NA+3…]
生成的;两者都可以像上面那样使用重塑来方便地构造。

您需要首先重塑
A
,使其与
B
一样宽,垂直连接
B
,然后将其重塑为原始高度

>> C = reshape([reshape(A, [], size(B,2)); B], size(B,1), [])
C =

   1   2   1   3   4   2
   5   6   3   7   8   4
*自然地假设
大小(A,1)==大小(B,1)


为了概括这一点,我们可以通过查找列数的gcd来查找每个数组的组数:

function C = groupcols(A, B)
% interleave columns of A and B such that
%   for m = number of columns of A, and n = number of columns of B
%   the output matrix C has alternating groups of
%     m / gcd(m,n) columns of A, followed by
%     n / gcd(m,n) columns of B
%   if gcd(m,n) == 1, then C will be all columns of A followed by
%     all columns of B

m = size(A,2);
n = size(B,2);
g = gcd(m,n);

C = [reshape(A, size(A,1)*m/g, []); reshape(B, size(B,1)*n/g, [])];
C = reshape(C, size(A,1), []);
使用输入:

A =

   1   2   3   4   5   6   7   8   9
   1   2   3   4   5   6   7   8   9

B =

   11   12   13   14   15   16
   11   12   13   14   15   16
输出为:

1    2    3   11   12    4    5    6   13   14    7    8    9   15   16
1    2    3   11   12    4    5    6   13   14    7    8    9   15   16

由于9列和6列的最大公约数是3,因此有3组,A中有3列,B中有2列。

您需要首先重塑
A
,使其与
B
一样宽,垂直连接
B
,然后重塑为原始高度

>> C = reshape([reshape(A, [], size(B,2)); B], size(B,1), [])
C =

   1   2   1   3   4   2
   5   6   3   7   8   4
*自然地假设
大小(A,1)==大小(B,1)


为了概括这一点,我们可以通过查找列数的gcd来查找每个数组的组数:

function C = groupcols(A, B)
% interleave columns of A and B such that
%   for m = number of columns of A, and n = number of columns of B
%   the output matrix C has alternating groups of
%     m / gcd(m,n) columns of A, followed by
%     n / gcd(m,n) columns of B
%   if gcd(m,n) == 1, then C will be all columns of A followed by
%     all columns of B

m = size(A,2);
n = size(B,2);
g = gcd(m,n);

C = [reshape(A, size(A,1)*m/g, []); reshape(B, size(B,1)*n/g, [])];
C = reshape(C, size(A,1), []);
使用输入:

A =

   1   2   3   4   5   6   7   8   9
   1   2   3   4   5   6   7   8   9

B =

   11   12   13   14   15   16
   11   12   13   14   15   16
输出为:

1    2    3   11   12    4    5    6   13   14    7    8    9   15   16
1    2    3   11   12    4    5    6   13   14    7    8    9   15   16
因为9列和6列的最大公约数是3,所以有3个组,其中3列来自A,2列来自B。

是否有特定的模式