필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Josefine Olsson
Josefine Olsson 2019년 11월 15일
답변: Athul Prakash 2019년 11월 18일
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
Josefine Olsson
Josefine Olsson 2019년 11월 15일
Hi! Thanks for helping out! With the ; it give me any output tho.
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