필터 지우기
필터 지우기

Vectorize 3 for loops

조회 수: 1 (최근 30일)
Robin L.
Robin L. 2019년 3월 20일
댓글: Robin L. 2019년 3월 20일
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;
else
tab(a_idx, b_idx, c_idx) = second_tab(a_idx, b_idx, c_idx) * 5;
end
end
end
end
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....
Robin

채택된 답변

Raghunandan V
Raghunandan V 2019년 3월 20일
편집: Raghunandan V 2019년 3월 20일
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;
new_tab(tab>Max)= inf;
I think this is pretty straightforward. Please reply if explanation is required
  댓글 수: 3
Raghunandan V
Raghunandan V 2019년 3월 20일
Yup. I will change it!
Robin L.
Robin L. 2019년 3월 20일
Raghunandan V I thought something like that existed but I didn't manage to get it works. So thank you so much !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by