Vectorize 3 for loops
2 views (last 30 days)
Show older comments
Commented: Robin L. on 20 Mar 2019
Hello the community !
% let us consider tab, a 3D-matrix, and second_tab, another one
max = 5;
for a_idx = 1:50
for b_idx = 1:60
for c_idx = 1:10
if (tab(a_idx, b_idx, c_idx) > max)
tab(a_idx, b_idx, c_idx) = Inf;
tab(a_idx, b_idx, c_idx) = second_tab(a_idx, b_idx, c_idx) * 5;
Do you guys know how I could optimize this processus, using Matlab vectorization, (permute, ones, reshape, .*, ./, an. so on.) ?
I can manage similarly cases but with the "if statement" I am blocked....
Raghunandan V on 20 Mar 2019
Edited: Raghunandan V on 20 Mar 2019
Hi, Here is your answer:
Max = 5;
tab = randi(10,50,60,10);
second_tab = randi(5,50,60,10);
new_tab = second_tab*5;
I think this is pretty straightforward. Please reply if explanation is required
More Answers (0)
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!