Filter löschen
Filter löschen

Another odd/even dilemma

3 Ansichten (letzte 30 Tage)
Jacque
Jacque am 15 Feb. 2012
I'm new to MATLAB but last year I got a lot of experience in VBA so I'm not completely untrained in code.
I have code which is running a 'for' loop controlling time values.
I have 4 sets of near exact code, but with slight edits just for classification.
Basically I'm simulating 4 cylinders and I need to display their movement, and my fourth cylinder I want to use to simulate vibration.
My query is about how I can go about saying:
"if time is odd here, position head here, else position head here"
This seems real simple but I'm having trouble because I don't have much experience with 'for' loops.
Any response would be greatly appreciated

Antworten (2)

Andrei Bobrov
Andrei Bobrov am 15 Feb. 2012
t = 1:20 % "your time"
% "a" - array (vector(1x20)) of position
% "1" - position for odd time
% "2" - position for even time
a = zeros(size(t))
a(1:2:end) = 1; % odd
a(2:2:end) = 2; % even

Jacque
Jacque am 15 Feb. 2012
Thank you for your response
So how could I integrate this into my code?
I'll show you the code that I'm using and can you see if you can apply it because from reading the code you've posted I can't really see how I can get the output to effect my code:
for T4 = 0:t:Tf % Cylinder 4
V14 = V14initial + (X04*A1); V24 = V24initial + ((0.17 - X04)*A2);
% MoveDistance1 = input('Enter 1st displacement: '); % MoveDistance4 = input('Enter 2nd displacement: '); % MoveDistance4 = input('Enter 3rd displacement: ');
if mod(T4,2) Xd4 = 0.05; else Xd4 = 0.09; end
... end
This is the section of code that I'm interested in. Btw the 'mod' was merely a test and I have no clue how to use this function

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by