[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Building a multidimensional block diagonal matrix avoiding loops
From: |
PetrSt |
Subject: |
Re: Building a multidimensional block diagonal matrix avoiding loops |
Date: |
Tue, 16 Apr 2013 06:10:23 -0700 (PDT) |
Try this:
clear all
A = rand([3,3,3])
[l,m,n] = size(A);
% Block diagonal in third dim
a=l; b=m*n; c=n;
B = zeros([a,b,c]);
ida = repmat((1:a)',m*n,1);
idb = reshape(repmat((1:b),l,1),l*m*n,1);
idc = reshape(repmat((1:c),l*m,1),l*m*n,1);
idx = sub2ind([a,b,c],ida,idb,idc);
B(idx) = A(:)
% Block diagonal in all dims
a=l*n; b=m*n; c=n;
B = zeros([a,b,c]);
ida = reshape(rotdim(reshape(repmat((1:a)',1,m),l,m,n),1,[2,3]),l*m*n,1);
idb = reshape(repmat((1:b),l,1),l*m*n,1);
idc = reshape(repmat((1:c),l*m,1),l*m*n,1);
idx = sub2ind([a,b,c],ida,idb,idc);
B(idx) = A(:)
--
View this message in context:
http://octave.1599824.n4.nabble.com/Building-a-multidimensional-block-diagonal-matrix-avoiding-loops-tp4651767p4651919.html
Sent from the Octave - General mailing list archive at Nabble.com.