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!
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!