Is there a way to vectorize this for loop to speed up?

1 view (last 30 days)
Hello
Is there a way to vectorize this for loop to speed up?
thank you
for j =1 :size(Rond_Input2Cell,1)
for k=1: size(Rond_Input2Cell,2)
Rond_Input2Cell(j,k)= (Pre_Rond_Input2Cell(j,k)*Y_FGate(k))+(net_Cell(k)*Y_InGate(k)*tmp_input(j)) ;
end
end
Matrixs size:
Rond_Input2Cell =39*120
Pre_Rond_Input2Cell = 39*120
Y_FGate=1*120 (row vector)
net_Cell=1*120 (row vector)
Y_InGate =1*120 (row vector)
tmp_input =1*39 (row vector)

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 17 Jun 2016
Edited: Andrei Bobrov on 17 Jun 2016
for j = 1 : size(R,1)
for k = 1 : size(R,2)
R(j,k)= (PR(j,k)*YF(k))+(C(k)*YI(k)*t(j)) ;
end
end
Matrixs size:
R =39*120
PR = 39*120
YF=1*120 (row vector)
C=1*120 (row vector)
YI =1*120 (row vector)
t =1*39 (row vector)
solution:
R = bsxfun(@times,PR,YF) + t(:)*(C.*YI)
  4 Comments
armita mani
armita mani on 17 Jun 2016
thank you, Is there another replacement way without bsxfun, for example like second part "t(:)*(C.*YI)"?
Andrei Bobrov
Andrei Bobrov on 17 Jun 2016
Edited: Andrei Bobrov on 17 Jun 2016
"t(:)*(C.*YI)"?
only for vectors (column and rows) and for @times. Example:
>> a = 1:5
a =
1 2 3 4 5
>> b = 10:10:40
b =
10 20 30 40
>> a(:)*b
ans =
10 20 30 40
20 40 60 80
30 60 90 120
40 80 120 160
50 100 150 200
>>

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!