adding multiple arrays in a loop
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Shrishti Yadav
le 11 Avr 2023
Commenté : Shrishti Yadav
le 11 Avr 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 commentaires
Réponse acceptée
Joe Vinciguerra
le 11 Avr 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!