I have a piece of code that works fine. But it has a for loop as below:
M=5;
N=10;
abc=0.0;
for m1=1:M*N
abc=abc+(abs(yo(m1,1)-ye(m1,1))).^2;
end
abc=abc/(M*N);
e=abc;
What will be its vectored form to reduce the execution time?

 채택된 답변

Matt J
Matt J 2022년 11월 21일
편집: Matt J 2022년 11월 21일

0 개 추천

e=norm(yo-ye).^2/M/N

댓글 수: 4

Matt J
Matt J 2022년 11월 21일
편집: Matt J 2022년 11월 21일
Thank you both dear Torsten and Matt J for your kind responses.
You're quite welcome. If the answer addresses your post, please Accept-click it.
Here is the complete script, how will I reduce its computational time?
Perhaps as below:
yo = yMatTR(deg2rad(u), steerVecT, steerVecR);
ye = yMatTR(deg2rad(b), steerVecT, steerVecR);
e=norm(yo-ye).^2/(M*N);
function y = yMatTR(targetAngle, steerVecT, steerVecR)
steerA = steerVecT(targetAngle);
steerB = steerVecR(targetAngle);
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end
Thank you dear Matt J for your help. Indeed it works. And I am going to accept your answer. But before acceptance, can you make me understand on the line:
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
Also is it true for any lenght of vector u?
Regards,
Matt J
Matt J 2022년 11월 21일
편집: Matt J 2022년 11월 21일
Also is it true for any lenght of vector u?
Yes, try it.
can you make me understand on the line:
Perhaps an example,
steerA=randi(5,3,2)
steerA = 3×2
5 4 4 2 5 2
steerB=randi(50,3,2);
%original version
steerM = zeros(size(steerA, 1)*size(steerB, 1), size(steerA,2));
for idxK = 1 : size(steerM,2)
steerM(:, idxK) = kron(steerB(:, idxK), steerA(:, idxK));
end
%new version
steerM2=steerA.*permute(steerB,[3,2,1]);
%compare
steerM,steerM2
steerM = 9×2
30 176 24 88 30 88 160 104 128 52 160 52 160 20 128 10 160 10
steerM2 =
steerM2(:,:,1) = 30 176 24 88 30 88 steerM2(:,:,2) = 160 104 128 52 160 52 steerM2(:,:,3) = 160 20 128 10 160 10
Sadiq Akbar
Sadiq Akbar 2022년 11월 22일
Thanks a lot dear Matt J for your kind help.Regards,

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2022년 11월 21일

댓글:

2022년 11월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by