adding multiple arrays in a loop

5 visualizaciones (últimos 30 días)
Shrishti Yadav
Shrishti Yadav el 11 de Abr. de 2023
Comentada: Shrishti Yadav el 11 de Abr. de 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])

Respuesta aceptada

Joe Vinciguerra
Joe Vinciguerra el 11 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by