Saturday, 15 March 2014

matlab - Finding the diagonal from a collapsed 3D matrix -



matlab - Finding the diagonal from a collapsed 3D matrix -

i have 3d vectors v.

a = rand(2, 2, 2); v = sum(a, 2);

now did:

b = diag(v); error using diag first input must 2d.

with loop, did following:

for = 1:2 b{i} = diag(v(:, :, i)); end

i 3d matrices 3d vectors. suppose have next vector:

v(:, :, 1)=[1 2 3]'; v(:, :, 2)=[1 2 4]'; %i get, using command , without loop (if possible), 3d matrix b b(:, :, 1)=[1 0 0; 0 2 0; 0 0 3]; b(:, :, 2)=[1 0 0; 0 2 0; 0 0 4];

i assuming your final lines in question have v , looking b without loops. same, think work -

%// input v(:, :, 1)=[1 2 3]'; v(:, :, 2)=[1 2 4]'; [m,~,p] = size(v) b = zeros(size(v,1),size(v,1),size(v,3)); b(bsxfun(@plus,[1:m+1:m*m]',[0:p-1]*m*m)) = v %//'

output -

b(:,:,1) = 1 0 0 0 2 0 0 0 3 b(:,:,2) = 1 0 0 0 2 0 0 0 4

matlab matrix vectorization diagonal

No comments:

Post a Comment