adding multiple arrays in a loop

5 次查看(过去 30 天)
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
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])
Also, instead of
for i = 1:3
you can do
for i = N1

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by