Filter löschen
Filter löschen

Help with for loop and invalid expression

1 Ansicht (letzte 30 Tage)
Az Naqvi
Az Naqvi am 6 Aug. 2021
Beantwortet: Dave B am 6 Aug. 2021
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')

Antworten (1)

Dave B
Dave B am 6 Aug. 2021
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!

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by