Diagonal slices of 3D array
31 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I have a 3D matrix C of size 2N×N×N and I need to take the diagonal slices of C and construct another 2D and 1D arrays with it. Like
D(i,j)=C(i,i,j)
D(i,j)=C(i,j,j)
D(i)=C(i,i,i)
And vice versa. If I have 1D and 2D arrays, how can I add them as diagonal slices to 3D matrix?
Is there a way to do it avoiding loops?
2 Commenti
James Tursa
il 25 Set 2020
Please show small complete examples showing input(s) and desired output(s).
Risposte (2)
Ameer Hamza
il 25 Set 2020
Modificato: Ameer Hamza
il 25 Set 2020
The question is not entirely clear, so the following is my guess, but such a problem can be solved using sub2ind().
C = % 2N*N*N
rows = 1:2*N;
cols = 1:N;
pages = 1:N;
idx = sub2ind(size(C), repelem(rows, 1, N), repmat(cols, 1, 2*N), repmat(pages, 1, 2*N));
D = reshape(C(idx), N, 2*N).'
2 Commenti
Ameer Hamza
il 26 Set 2020
Try using tic and toc to find the execution time in both cases. I guess the vectorized will at least be equivalent, if not faster, then the looped version.
Bruno Luong
il 26 Set 2020
Modificato: Bruno Luong
il 26 Set 2020
[I,J] = ndgrid(1:N);
D1 = zeros(2*N,N);
D1(sub2ind(size(D1),I,J)) = C(sub2ind(size(C),I,J,J));
D2 = zeros(2*N,N);
D2(sub2ind(size(D2),I,J)) = C(sub2ind(size(C),I,I,J));
D2(sub2ind(size(D2),I+N,J)) = C(sub2ind(size(C),I+N,I,J));
0 Commenti
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!