How can I implement this for loop in vector operation form?
    8 ビュー (過去 30 日間)
  
       古いコメントを表示
    
I have this for loop that I would like to implement more efficiently
clear all;
clearvars;
N = 5;
Rs = 100*10^6;
Ts = 1/Rs;
J = 4;
Tsamp = Ts/J;
x = 2.*randi([0 1], N, 1)-1;
s = repelem(x,J);
n=0:N*J-1;
t = Tsamp.*n;
x_t=0;
%% This is the for loop
for nn=1:length(n)
    x_t = x_t+ s(nn).*sinc(n-nn); 
end
plot(t, x_t)
How can I do this?
0 件のコメント
回答 (1 件)
  Anuj
      
 2023 年 7 月 10 日
        Hi Mawe,
You can use parallel computing toolbox to use parfor to run the for loop parallelly, which would extensively reduce the computation time.
refer to this article - Parallel for-Loops (parfor) - MATLAB & Simulink - MathWorks India
or you can vertorize the inner for loop step with the following code - 
x_t = sum(s .* sinc(n - (1:length(n))));
By vectorizing the code, we avoid the need for an explicit for loop, which can improve the execution speed and efficiency of the code.
NOTE - The vectorized version assumes that the dimensions of s and n are compatible for element-wise operations.
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!

