how to use vectorized language or any other methods to replace the following code, in order to make the computation faster

2 次查看(过去 30 天)
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!

采纳的回答

jgg
jgg 2016-1-12
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!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by