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

1 次查看(过去 30 天)
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)

采纳的回答

madhan ravi
madhan ravi 2019-7-12
% 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 个)

类别

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

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by