how to use vectorized language or any other methods to replace the following code, in order to make the computation faster
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
following are my current code:
if true
% code
tic;
S = size(P,1);
Q2 = -permute(P,[2 3 1]);
for i = 1:S
Q2(i,:,i) = Q2(i,:,i) + 1;
end
Q2 = reshape(Q2,S,[]);
toc
end
P is the transition probability matrix,
if true
% code
P(:,:,1)=[3/4 1/4;3/4 1/4];
P(:,:,2)=[1/4 3/4;1/4 3/4];
end
My problem is that when the dimension of P is larger than 8000*8000*8,the current computation will be very slow.Is there anyone who know how to optimize the current code? Thank you in advance!
0 Kommentare
Akzeptierte Antwort
jgg
am 12 Jan. 2016
I think this should work. Replace your for loop with:
i = [1:S]
Q2(i,:,i) = Q2(i,:,i) + 1;
This works on the test case you posed, as far as I can tell, so I think it should work on your much bigger data. Let me know!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!