How do I use two nested for loops to store the calculated values from a given equation in a 2 dimensional array?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Shreyang Prajapati
le 12 Juil 2019
Réponse apportée : madhan ravi
le 12 Juil 2019
When I run this, it reads the following error
"Unable to perform assignment
because the size of the left side
is 1-by-1 and the size of the
right side is 1-by-5."
Error in LabAssignment1 (line 13)
X(i,j)= 3*cos(2*pi*t.*f +
0.1);
M=5;
N=5;
t=[0,0.1,0.2,0.3,0.4];
f=[0,0,10,15,20];
X=zeros(M,N);
for i=1:M
for j=1:N
X(i,j)= 3*cos(2*pi*t.*f + 0.1);
end
end
disp(X)
0 commentaires
Réponse acceptée
madhan ravi
le 12 Juil 2019
% With Loop
t=[0,0.1,0.2,0.3,0.4];
f=[0,0,10,15,20];
M = numel(t);
N = numel(f);
X=zeros(M,N);
for ii = 1:M
for jj = 1:N
X(ii,jj) = 3*cos(2*pi*t(ii).*f(jj) + 0.1);
end
end
disp(X)
% Without Loop
[T,F]=ndgrid(t,f);
X = 3*cos(2*pi*T.*F + 0.1);
0 commentaires
Plus de réponses (0)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!