hi i'd like to know if there's is a way to speed up this algoritm where:, lphi is a vector of 120 elements, IN4 is a matrix of 4x8192, aaa is a 3D matrix of 4x8192x120
for k = 1:lphi;
B4(k,:) = sum(IN4.*aaa(:,:,k));
end
B = sum(abs(B4).^2,2);
thanks, i don't know if in c++ it work faster any ideas?

댓글 수: 2

the cyclist
the cyclist 2012년 2월 13일
It's not clear to me what your loop does. You say that lphi is a vector, but then you loop over k=1:lphi. That seems odd to me, and not really what you want.
Paolo Rossi
Paolo Rossi 2012년 2월 13일
Sorry, lphi is not a vector is the length of a vector lphi = 120

댓글을 달려면 로그인하십시오.

 채택된 답변

Jonathan Sullivan
Jonathan Sullivan 2012년 2월 13일

0 개 추천

B4 = reshape(permute(bsxfun(@tims,IN4,aaa),[3 1 2]),size(aaa,3),[]);
B = sum(abs(B4).^2,2);

댓글 수: 4

Paolo Rossi
Paolo Rossi 2012년 2월 13일
In your way it takes about 0.3 sec to calculate,
in my way it takes about 0.1 sec
thanks anyway friend
i'd like to have 0.05 sec ... for real time processing, i don't know if in c++ it is more faster
Sean de Wolski
Sean de Wolski 2012년 2월 13일
I believe, Jonathan meant @times
for the bsxfun call.
Sean de Wolski
Sean de Wolski 2012년 2월 13일
And it still doesn't give the correct results.
Sean de Wolski
Sean de Wolski 2012년 2월 13일
Like, I said, this does not produce correct results, nor does it work as written. PERMUTEing a big array will likely never be faster than a well written for-loop.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2012년 2월 13일

0 개 추천

bsxfun is lurking (update: and staying hidden)!
The fastest I could get it was to dynamically preallocate your for-loop.
for k = 120:-1:1
B5(k,:) = sum(IN4.*aaa(:,:,k));
end
B2 = sum(abs(B5).^2,2);
James Tursa
James Tursa 2012년 2월 13일

0 개 추천

Another option you can try using some FEX submissions (slightly better at memory management than your original code):
uninit; % One time only, to built the mex routine
mtimesx; % One time only, to build the mex routine
B4 = uninit(8192,1,lphi); % Fast create variable
for k = 1:lphi;
B4(:,:,k) = sum(IN4.*aaa(:,:,k)).'; % Contiguous result
end
B = mtimesx(B4,'t',B4,'speedomp'); % Fast nD dot product
B = B(:); % Reshape result to column
You can find uninit and mtimesx here:
Note: This won't give exactly the same answer as the MATLAB code because the last dot product operation is done internally in mtimesx differently from MATLAB.
Q: Is everything real? What is the point of the abs( )?

댓글 수: 3

Paolo Rossi
Paolo Rossi 2012년 2월 13일
No it's a complex Data, the point in the abs is becouse every elements must be elevated at 2
now i try your code thanks and then i will write the results
James Tursa
James Tursa 2012년 2월 13일
In that case use 'c' instead of 't' in the mtimesx call. E.g.,
B = mtimesx(B4,'c',B4,'speedomp');
Paolo Rossi
Paolo Rossi 2012년 2월 13일
Thanks but the bottle neck is in the loop, infact the loop takes 0.1 sec...may be this is the best performance, i will try anyway your functions

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

질문:

2012년 2월 13일

편집:

2013년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by