필터 지우기
필터 지우기

Vectorization of nested for loop

조회 수: 1 (최근 30일)
Ilaria
Ilaria 2023년 10월 12일
편집: Ilaria 2023년 12월 8일
Hello,
I am trying to vectorise a piece of code. The code is the following:
main_calc=zeros(N,length(time)); %initialize main calculation
for wall_send=1:length(6) %sending wall
for send=1+lenght(wall_send):lenght(wall_send+1) %elements on the sending wall
for wall_rec=1:length(6) %receiving wall
for rec=1+lenght(wall_send):length(wall_send+1) %elements of receiving wall
main_calc(rec,time_step_ij)=main_calc(rec,time_step_ij)+previous_calc(send,time_step_ij); %new calc
end
end
end
end
previous_calc=main_calc; %update
How do I vectorize the above code?
Thank you
  댓글 수: 1
Rena Berman
Rena Berman 2023년 12월 5일
(Answers Dev) Restored edit

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

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 10월 12일
I don't think what you are doing is a vectorization. Here is a simple demo of vectorization :
% Loop
x = linspace(0, 2*pi);
y = linspace(-pi, pi);
Z = zeros(numel(x), numel(y));
for ii = 1:numel(x)
for jj=1:numel(y)
Z(ii,jj) = sin(x(ii))+cos(y(jj));
end
end
figure(1)
mesh(Z)
title('Computed with a loop')
% Vectorization
x = linspace(0, 2*pi);
y = linspace(-pi, pi);
[X, Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
figure(2)
mesh(Z')
title('Computed with a vectorization')
  댓글 수: 1
Ilaria
Ilaria 2023년 10월 16일
i am not sure this does help me. What should I do in my case?

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by