MATLAB Answers

Use only defined 4D array elements

1 view (last 30 days)
Mantas Vaitonis
Mantas Vaitonis on 22 Jul 2018
Commented: Mantas Vaitonis on 22 Jul 2018
Hello to All, There is 4D array varcov (NxNxZxL), I need to make other 4D array with the following values:
varcov1=[varcov(1,1,:,:) varcov(2,2,:,:) varcov(3,3,:,:) ..till end(N)];
That result varcov1 would be (1xNxZxL).


Sign in to comment.

Accepted Answer

Rik on 22 Jul 2018
The code below should work for you. Since R2016b you can use the line with implicit expansion.
varcov=reshape(1:(N*N*Z*L),N,N,Z,L);%generate sample data
inds=bsxfun(@plus,(1:N+1:N^2)',N^2*(0:(Z*L-1)));%find the inds to the diagonal of each slice
%(1:N+1:N^2)'+N^2*(0:(Z*L-1)); with implicit expansion

  1 Comment

Mantas Vaitonis
Mantas Vaitonis on 22 Jul 2018
Yes this works, thank you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by