Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Can any one explain me how to apply vectorized operation on the outer for loop of the given program?

3 ビュー (過去 30 日間)
Tamil selvan
Tamil selvan 2016 年 11 月 17 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
a=[1+1i 2+2i 3+3i 4+4i 5+5i 6+6i 7+7i 8+8i 9+9i 1+1i 2+2i 3+3i 4+4i 5+5i 6+6i 7+7i 8+8i 9+9i];
for m=1:6
m=m-1;
k=1:1:4;
out_1(m+1)=(sum((a(k+m)).*(conj(a(k+12+m)))))/12;
end
  2 件のコメント
Alexandra Harkai
Alexandra Harkai 2016 年 11 月 17 日
Does this code not give you an indexing error for a(k+12+m) after the 3rd loop? (k+12+3 = 1:4+12+3=16:19 and a has only 18 elements)
Tamil selvan
Tamil selvan 2016 年 11 月 17 日
Hello Alexandra Harkai,
Sorry. Just now I seen it is as 12 instead of 8.

回答 (1 件)

Guillaume
Guillaume 2016 年 11 月 17 日
As Alexandra pointed out, your example is indexing past the end of the array.
Assuming a sufficiently large array, the following would work:
%input: %a, a vector with enough elements
%e.g:
a = repmat((1:9).*(1+1i), 1, 3)
m = 1:6; %row vector
k = (0:3).'; %column vector
assert(numel(a) >= max(m) + max(k) + 12, 'Not enough elements in a');
%in R2016b:
out = sum(a(m+k) .* conj(a(m+k+12))) / 12;
%prior to R2016b:
%indices = bsxfun(@plus, m, k);
%out = sum(a(indices) .* conj(a(indices+12))) / 12;
  1 件のコメント
Tamil selvan
Tamil selvan 2016 年 11 月 17 日
編集済み: Guillaume 2016 年 11 月 17 日
Hello Guillaume,
Thanks for your code its working fine. When I am checking 100000 samples input, the execution time for the above code it takes 4times more than the original code had taken. Can you tell me any other execution time optimization for the above code?

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by