vectorize calculation of 3 for
Show older comments
Hello !
Let us take 3 vectors 1D :
vectors L, M and N
% let us take L, M, N, 3 matrix 1D
for Lidx = 1:length(L)
for Midx = 1:length(M)
for Nidx = 1:length(N)
tab(Lidx, Midx, Nidx) = L(Lidx)*M(Midx)*N(Nidx);
end
end
end
How can I optimize that ?
I found myself a solution :
L_repmat = repmat(L, 1, length(M), length(N));
N_3d = permute(N, [3 1 2]);
N_3d_repmat = repmat(N_3d, length(L), length(M));
vectorized = L_repmat.*N_3d_repmat;
I works nicely, but is there a more optimized way ?
Robin ?
Accepted Answer
More Answers (0)
Categories
Find more on Earth and Planetary Science in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!