How do I use two nested for loops to store the calculated values from a given equation in a 2 dimensional array?

1 Ansicht (letzte 30 Tage)
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)

Akzeptierte Antwort

madhan ravi
madhan ravi am 12 Jul. 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);

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by