Block diagonalization of rows of matrix J-by-2

Given the J-by-2 matrix , for example

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

I want to block his diagonalization. That is, I want:

B = [1 2 0 0 0 0 ; 0 0 3 4 0 0 ; 0 0 0 0 5 6].

One team that does this:

blkdiag(A(1,:),A(2,:),A(3,:))

It will be slow and tiring if Jgreat. Is there a built-in Matlab function that does this?

+4
source share
4 answers

Here's one hacky solution for an array J x 2using linear indexing-

%// Get number of rows
N = size(A,1);                   

%// Get linear indices of the first column elements positions in output array
idx = 1:2*N+1:(N-1)*(2*N+1)+1;   

%// Setup output array
out = zeros(N,N*2);

%// Put first and second column elements into idx and idx+N positions
out([idx(:) idx(:)+N]) = A

There is only one function call (neglecting size, since it should be minimal) overhead zeros, and even this can be removed with this undocumented zeros initialization trick-

out(N,N*2) = 0; %// Instead of out = zeros(N,N*2);

Run Example -

A =
     1     2
     3     4
     5     6
     7     8
out =
     1     2     0     0     0     0     0     0
     0     0     3     4     0     0     0     0
     0     0     0     0     5     6     0     0
     0     0     0     0     0     0     7     8

Here's a comparative analysis of published solutions.

Benchmarking code

%//Set up some random data
J = 7000;   A = rand(J,2);
%// Warm up tic/toc
for k = 1:100000
    tic(); elapsed = toc();
end   
disp('---------------------------------- With @mikkola solution')
tic
temp = mat2cell(A, ones(J,1), 2);
B = blkdiag(temp{:});
toc, clear B temp
disp('---------------------------------- With @Jeff Irwin solution')
tic
m = size(A, 1);
n = size(A, 2);
B = zeros(m, m * n);
for k = 1: n
    B(:, k: n: m * n) = diag(A(:, k));
end
toc, clear B k m n
disp('---------------------------------- With Hacky1 solution')
tic
N = size(A,1);                   
idx = 1:2*N+1:(N-1)*(2*N+1)+1;   
out = zeros(N,N*2);
out([idx(:) idx(:)+N]) = A;
toc, clear out idx N
disp('---------------------------------- With Hacky2 solution')
tic
N = size(A,1);                   
idx = 1:2*N+1:(N-1)*(2*N+1)+1;
out(N,N*2) = 0;
out([idx(:) idx(:)+N]) = A;
toc, clear out idx N

Runtimes

---------------------------------- With @mikkola solution
Elapsed time is 0.546584 seconds.
---------------------------------- With @Jeff Irwin solution
Elapsed time is 1.330666 seconds.
---------------------------------- With Hacky1 solution
Elapsed time is 0.455735 seconds.
---------------------------------- With Hacky2 solution
Elapsed time is 0.364227 seconds.
+5

mat2cell J-by-1, A. {:}, blkdiag , :

%//Set up some random data
J = 100;
A = rand(J,2);
%// Solution for arbitrary J-by-2 A
temp = mat2cell(A, ones(J,1), 2);
B = blkdiag(temp{:})

! , , @Divakar. .

---------------------------------- With @mikkola solution
Elapsed time is 0.100674 seconds.
---------------------------------- With @Jeff Irwin solution
Elapsed time is 0.283275 seconds.
---------------------------------- With @Divakar Hacky1 solution
Elapsed time is 0.079194 seconds.
---------------------------------- With @Divakar Hacky2 solution
Elapsed time is 0.051629 seconds.
+4

. , , A .

A = [1 2; 3 4; 5 6]
m = size(A, 1)
n = size(A, 2)

B = zeros(m, m * n)
for k = 1: n
    B(:, k: n: m * n) = diag(A(:, k))
end
+3

, sparse, , :

N = size(A,1);
ind_1 = [1:N].';
ind_2 = [1:2:2*N-1].';
A_1   = sparse(ind_1,ind_2,A(:,1),N,2*N);
ind_2 = [2:2:2*N].';
A_2   = sparse(ind_1,ind_2,A(:,2),N,2*N);
out       = A_1 + A_2;

, @Divakar:

---------------------------------- With @mikkola solution
Elapsed time is 0.065136 seconds.
---------------------------------- With @Jeff Irwin solution
Elapsed time is 0.500264 seconds.
---------------------------------- With Hacky1 solution
Elapsed time is 0.200303 seconds.
---------------------------------- With Hacky2 solution
Elapsed time is 0.011991 seconds.
---------------------------------- With @Matt T solution
Elapsed time is 0.000712 seconds.
0

Source: https://habr.com/ru/post/1617289/


All Articles