How to create a symmetric matrix where each row / column is a subset of a known vector

I have a 7 * 1 vector a = (1:7).'

. I want to form A

a 4 * 4 matrix from a vector A

so that the elements A

form the antidiagonal of the matrix A

like this:

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

      

I would like this to work for general A

, not just when the elements are whole integers.

I appreciate any help.

+3


source to share


2 answers


You can use hankel

:

n= 4;
A= hankel(a(1:n),a(n:2*n-1))

      

Another solution (extension / bsxfun):

In MATLAB r2016b / Octave It can be created as:

A = a((1:4)+(0:3).')

      

In pre r2016b, you can use bsxfun

:



A = a(bsxfun(@plus,1:4, (0:3).'))

      

I / O example

a = [4 6 2 7 3 5 1]

A =

   4   6   2   7
   6   2   7   3
   2   7   3   5
   7   3   5   1

      

Using the test provided by @Wolfie tested in Octave:

 _____________________________________
|Method   |memory peak(MB)|timing(Sec)|
|=========|===============|===========|
|bsxfun   |2030           |1.50       |
|meshgrid |3556           |2.43       |
|repmat   |2411           |2.64       |
|hankel   |886            |0.43       |
|for loop |886            |0.82       |

      

+5


source


Setting up indexing

Adding two outputs meshgrid

can give indices:

[x, y] = meshgrid(1:4, 0:3);
x + y;
% ans = [1     2     3     4
%        2     3     4     5
%        3     4     5     6
%        4     5     6     7];

      

If it a

was the same as your example, you could stop there. Alternatively, use this to index a common vector a

. For comparison, I'll use the same input example as rahnema1 for my method:

a = [4 6 2 7 3 5 1];
[x, y] = meshgrid(1:4, 0:3);
A = a(x + y);    
% A = [4     6     2     7
%      6     2     7     3
%      2     7     3     5
%      7     3     5     1] 

      

There are many ways to create indexes instead of using them meshgrid

, see the benchmarking functions below for some examples!




Benchmarking and seven different methods.

Below are some points to run various methods, including using cumsum

, repmat

, hankel

and a simple cycle for

. This test was done in Matlab 2015b, so it takes advantage of Matlab optimizations, etc., which Octave tests in rahnema1's answer might not do. I also use a function timeit

that is more reliable than tic

/ toc

because it does multiple probes, etc.

function benchie()
    n = 10000;    % (large) square matrix size
    a = 1:2*n-1;  % array of correct size, could be anything this long
    f1 = @() m1(a,n);  disp(['bsxfun:   ', num2str(timeit(f1))]);
    f2 = @() m2(a,n);  disp(['cumsum:   ', num2str(timeit(f2))]);
    f3 = @() m3(a,n);  disp(['meshgrid: ', num2str(timeit(f3))]);
    f4 = @() m4(a,n);  disp(['repmat:   ', num2str(timeit(f4))]);
    f5 = @() m5(a,n);  disp(['for loop: ', num2str(timeit(f5))]);
    f6 = @() m6(a,n);  disp(['hankel1:  ', num2str(timeit(f6))]); 
    f7 = @() m7(a,n);  disp(['hankel2:  ', num2str(timeit(f7))]); 
end
% Use bsxfun to do broadcasting of addition
function m1(a,n); A = a(bsxfun(@plus, (1:n), (0:n-1).')); end
% Use cumsum to do cumulative vertical addition to create indices
function m2(a,n); A = a(cumsum([(1:n); ones(n-1,n)])); end
% Add the two meshgrid outputs to get indices
function m3(a,n); [x, y] = meshgrid(1:n, 0:n-1); A = a(x + y); end
% Use repmat twice to replicate the meshgrid results, for equivalent one liner
function m4(a,n); A = a(repmat((1:n)',1,n) + repmat(0:n-1,n,1)); end
% Use a simple for loop. Initialise A and assign values to each row in turn
function m5(a,n); A = zeros(n); for ii = 1:n; A(:,ii) = a(ii:ii+n-1); end; end
% Create a Hankel matrix (constant along anti-diagonals) for indexing
function m6(a,n); A = a(hankel(1:n,n:2*n-1)); end
% Create a Hankel matrix directly from elements
function m7(a,n); A = hankel(a(1:n),a(n:2*n-1)); end

      

Output:

bsxfun:   1.4397 sec
cumsum:   2.0563 sec
meshgrid: 2.0169 sec
repmat:   1.8598 sec
for loop: 0.4953 sec % MUCH quicker!
hankel1:  2.6154 sec
hankel2:  1.4235 sec

      

So your best bet is to use rahnema1's suggestion bsxfun

or direct matrix creation hankel

if you want a single liner, here is a brilliant StackOverflow answer that explains some of the benefits bsxfun

: In Matlab, when is it optimal to use bsxfun?

However, the for loop is more than twice as fast! Conclusion: Matlab has many neat ways to achieve this kind of thing, sometimes a simple loop with some appropriate pre-placement and internal Matlab Optimization might be the fastest.

+6


source







All Articles