how to use vectorized language or any other methods to replace the following code, in order to make the computation faster
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
jgg
on 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!
More Answers (0)
See Also
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!