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

1 view (last 30 days)
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)

Accepted Answer

madhan ravi
madhan ravi on 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);

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!