adding multiple arrays in a loop
5 views (last 30 days)
Show older comments
Shrishti Yadav
on 11 Apr 2023
Commented: Shrishti Yadav
on 11 Apr 2023
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])
0 Comments
Accepted Answer
Joe Vinciguerra
on 11 Apr 2023
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])
Also, instead of
for i = 1:3
you can do
for i = N1
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!