필터 지우기
필터 지우기

Help with for loop and invalid expression

조회 수: 1 (최근 30일)
Az Naqvi
Az Naqvi 2021년 8월 6일
답변: Dave B 2021년 8월 6일
Hi, I'm currently stuck on this loop and I'm getting an inalid expression error at the moment. Would be greatful if someone could help me resolve it please.
m=458
n= 2
t = 30
for i=1:m
for j=1:n
x = 1.4*sin(t^1.6) + 0.3*tan(t^1.3)
t = t+0.01
a(i,j)=(t,x) %this is where the first error occurs
end
end
a=reshape(a,m,n)
figure
hold on
plot(a,Linespace)
title('Displacement vs time')
v = reshape(a,m,n);
b = reshape(a.m.n)
for i in x:
if(x(1,i) >20)
v = x(1,i)
else
b = x(1,i)
end
end
figure
hold on
plot(v,Linespace,'g')
plot(b,Linespace,'r')
title('Displacement vs time')

답변 (1개)

Dave B
Dave B 2021년 8월 6일
The line of code
a(i,j)=(t,x);
doesn't make sense. a(i,j) is a location in a matrix, namely the ith row and jth column of the matrix a. You can only store one value there. So you can do:
a(i,j) = t;
or
a(i,j) = x;
or even
a(i,j,1:2)=[1 2]; % if a was a 3 dimensional matrix
My guess is leave the t out of the loop, it looks like it's something like:
t = 0:.01:(numel(a)-1)*.01;
Once you've done that, you'll realize you can probably leave the assignment of the loop:
a = 1.4.*sin(t.^1.6) + 0.3.*tan(t.^1.3);
% but I'll leave it to you to figure out how big t should be without
% relying on numel(a). It's pretty simple!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by