# adding multiple arrays in a loop

13 ビュー (過去 30 日間)
Shrishti Yadav 2023 年 4 月 11 日
コメント済み: Shrishti Yadav 2023 年 4 月 11 日
I would like to run the following loop for N1 = 90 but I am not sure how to set it up so that the arrays stored in matrix V can be added in a loop. Does someone know a better way? Here is my code:
clc;
clear;
%% Signal Setup
% Setting up sampling parameters
Fs = 400e3; % Sampling rate
T = 1/Fs; % Sampling period
N = 40000; % Signal length
t = (0:N-1)*T; % Time vector
% Wave Setup
f1 = 8000; %Hz
N1 = 1:3;
f2 = (N1)*f1;
V = zeros(size(3,N));
for i = 1:3
A =(1/N1(i))*sin(2*pi*f2(i)*t);
V(i,1:length(t)) = A(:);
end
S = V(1,:) + V(2,:) + V(3,:);
plot(S)
xlim([0 500])

サインインしてコメントする。

### 採用された回答

Joe Vinciguerra 2023 年 4 月 11 日
Use
S = sum(V, 1);
So you end up like this:
clc;
clear;
%% Signal Setup
% Setting up sampling parameters
Fs = 400e3; % Sampling rate
T = 1/Fs; % Sampling period
N = 40000; % Signal length
t = (0:N-1)*T; % Time vector
% Wave Setup
f1 = 8000; %Hz
N1 = 1:90;
f2 = (N1)*f1;
V = zeros(size(3,N));
% for i = 1:3
for i = N1
A =(1/N1(i))*sin(2*pi*f2(i)*t);
V(i,1:length(t)) = A(:);
end
% S = V(1,:) + V(2,:) + V(3,:);
S = sum(V, 1);
plot(S)
xlim([0 500])
for i = 1:3
you can do
for i = N1
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Shrishti Yadav 2023 年 4 月 11 日
Thanks!

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by