How do I create a vector from a for loop with different intervals?

3 次查看(过去 30 天)
Hi all!
I am trying to get a vector out of this loop, but it just gives me a vector for every iteration.
Does anyone know how to solve this?
t1 = 0.6; %[sec]
po = 100; %[kip]
E = 3000; %[ksi]
m = 20/386; %[kip^2/in]
k = 36.2222;
wn = sqrt(k/m); %[rad/sec]
T = (2*pi)/wn; %[sec]
zeta = 0.02;
c = 2*zeta*m*wn; % Damping constant [lb*s/in]
nstep = (6*T)/0.05;
nsteps = int16(fix(nstep))
ivalues = [1:nsteps]';
tvalues = [0:0.05:6*T]';
%Excitation functions
p1 = zeros(nsteps,1);
for i = 1:length(tvalues)
if tvalues(i) < (t1/2)
p1(i) = (2*po*tvalues(i))/t1;
elseif tvalues(i) > (t1/2) & tvalues(i) < t1
p1(i) = 2*po*(1-(tvalues(i)/t1));
else
p1(i) = 0;
end
p1(i) = p1(i)
end % I need a 1x28 vector with the p values.
Thank you in advance!
Josefine
  3 个评论
Shubham Gupta
Shubham Gupta 2019-11-15
编辑:Shubham Gupta 2019-11-15
After the loop use "fprintf()" or "disp()" to diplay p1 vector. Or you can simply write p1 without ; to display it. semicolon (;) stops MATLAB from printng out data in command window just get a grasp when do you want MATLAB to show the results.

请先登录,再进行评论。

回答(1 个)

Athul Prakash
Athul Prakash 2019-11-18
Hey Josefine,
Everytime MATLAB executs the line
p1(i) = p1(i)
It prints the result, which in this case is the entire vector 'p1'.
This line gets you the output during every iteration.
My suggestion is to remove this line, since it doesn't serve another purpose, and use
disp(p1);
or simply
p1
after the for loop ends.

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by