多个常量到矩阵并在matlab中将它们转换为块对角矩阵

时间:2015-02-21 22:16:40

标签: matlab matrix vectorization diagonal

我有a1 a2 a3。它们是常数。我有一个矩阵A.我想要做的是获得a1 * A,a2 * A,a3 * A三个矩阵。然后我想将它们转换成对角块矩阵。对于三个常数的情况,这很容易。我可以让b1 = a1 * A,b2 = a2 * A,b3 = a3 * A,然后在matlab中使用blkdiag(b1,b2,b3)。

如果我有n个常数,a1 ...... an。我怎么能在没有任何循环的情况下做到这一点?我知道这可以通过kronecker产品完成,但这非常耗时,你需要做很多不必要的0 *常量。

谢谢。

3 个答案:

答案 0 :(得分:5)

讨论和代码

这可以是 bsxfun(@plus 的一种方法,以函数格式编码 linear indexing -

function out = bsxfun_linidx(A,a)
%// Get sizes
[A_nrows,A_ncols] = size(A);
N_a = numel(a);

%// Linear indexing offsets between 2 columns in a block & between 2 blocks
off1 = A_nrows*N_a;
off2 = off1*A_ncols+A_nrows;

%// Get the matrix multiplication results
vals = bsxfun(@times,A,permute(a,[1 3 2])); %// OR vals = A(:)*a_arr;

%// Get linear indices for the first block
block1_idx = bsxfun(@plus,[1:A_nrows]',[0:A_ncols-1]*off1);  %//'

%// Initialize output array base on fast pre-allocation inspired by -
%// http://undocumentedmatlab.com/blog/preallocation-performance
out(A_nrows*N_a,A_ncols*N_a) = 0; 

%// Get linear indices for all blocks and place vals in out indexed by them
out(bsxfun(@plus,block1_idx(:),(0:N_a-1)*off2)) = vals;

return;

如何使用:要使用上面列出的功能代码,请假设您拥有a1a2a3 ,. ...,an存储在向量a中,然后执行此类out = bsxfun_linidx(A,a)之类的操作,以便在out中获得所需的输出。

基准

本节将本答案中列出的方法与运行时性能的其他答案中列出的其他两种方法进行比较或基准测试。

其他答案转换为功能形式,如此 -

function B = bsxfun_blkdiag(A,a)
B = bsxfun(@times, A, reshape(a,1,1,[])); %// step 1: compute products as a 3D array
B = mat2cell(B,size(A,1),size(A,2),ones(1,numel(a))); %// step 2: convert to cell array
B = blkdiag(B{:}); %// step 3: call blkdiag with comma-separated list from cell array

function out = kron_diag(A,a_arr)
out = kron(diag(a_arr),A);

为了进行比较,我们测试了Aa四种尺寸组合,它们是 -

  • A500 x 500a1 x 10
  • A200 x 200a1 x 50
  • A100 x 100a1 x 100
  • A50 x 50a1 x 200

下面列出了使用的基准代码 -

%// Datasizes
N_a = [10  50  100 200];
N_A = [500 200 100 50];

timeall = zeros(3,numel(N_a)); %// Array to store runtimes
for iter = 1:numel(N_a)

    %// Create random inputs
    a = randi(9,1,N_a(iter));
    A = rand(N_A(iter),N_A(iter));

    %// Time the approaches
    func1 = @() kron_diag(A,a);
    timeall(1,iter) = timeit(func1); clear func1

    func2 = @() bsxfun_blkdiag(A,a);
    timeall(2,iter) = timeit(func2); clear func2

    func3 = @() bsxfun_linidx(A,a);
    timeall(3,iter) = timeit(func3); clear func3
end

%// Plot runtimes against size of A
figure,hold on,grid on
plot(N_A,timeall(1,:),'-ro'),
plot(N_A,timeall(2,:),'-kx'),
plot(N_A,timeall(3,:),'-b+'),
legend('KRON + DIAG','BSXFUN + BLKDIAG','BSXFUN + LINEAR INDEXING'),
xlabel('Datasize (Size of A) ->'),ylabel('Runtimes (sec)'),title('Runtime Plot')

%// Plot runtimes against size of a
figure,hold on,grid on
plot(N_a,timeall(1,:),'-ro'),
plot(N_a,timeall(2,:),'-kx'),
plot(N_a,timeall(3,:),'-b+'),
legend('KRON + DIAG','BSXFUN + BLKDIAG','BSXFUN + LINEAR INDEXING'),
xlabel('Datasize (Size of a) ->'),ylabel('Runtimes (sec)'),title('Runtime Plot')

我最终获得的运行时图是

enter image description here

enter image description here

结论:正如您所看到的,可以查看基于bsxfun的方法之一,具体取决于您要处理的数据类型!

答案 1 :(得分:5)

这是另一种方法:

  1. 使用bsxfun;
  2. 将产品计算为3D数组
  3. 转换为每个单元格中包含一个产品(矩阵)的单元格数组;
  4. 使用从单元格数组生成的blkdiag来调用comma-separated list
  5. A表示您的矩阵,a表示带有常量的向量。然后获得期望的结果B作为

    B = bsxfun(@times, A, reshape(a,1,1,[])); %// step 1: compute products as a 3D array
    B = mat2cell(B,size(A,1),size(A,2),ones(1,numel(a))); %// step 2: convert to cell array
    B = blkdiag(B{:}); %// step 3: call blkdiag with comma-separated list from cell array
    

答案 2 :(得分:3)

这是一种使用kron的方法,它似乎比Divakar基于bsxfun的解决方案更快,内存效率更高。我不确定这与你的方法有什么不同,但时间似乎相当不错。可能值得在不同方法之间进行一些测试,以便更有效地解决问题。

A=magic(4);

a1=1;
a2=2;
a3=3;

kron(diag([a1 a2 a3]),A)