Uning Only Interger Values

2 views (last 30 days)
JWalker
JWalker on 1 Dec 2018
Commented: madhan ravi on 1 Dec 2018
I am trying to index an array based on several values, using another array(Dt). This means I need to make sure all my values in Dt being used to index are interger values so I need to remove the values that are not intergers from Dt. Is there a way to do this directly?
Alternatively as Dt is being generated by Dt = Tf/nSteps; where Tf is a constant and nSteps = 1:1:20; is there a way to insure Dt only takes the interger values of Tf/nSteps?
Any help is appreciated.
  2 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 1 Dec 2018
Can you explain more with array and index example?
JWalker
JWalker on 1 Dec 2018
This is the section of code I'm working on currently, It doesn't have variable definitions but here:
for nSteps = 1:1:40
Dt = Tf/nSteps; %Makes Dt step size foe new steps
DtInt = floor(Dt); %Makes Dt Intergers
DtInt= unique(DtInt); %Makes unique intergers
T0=T; %Resets start temp of object
EulerTemp = zeros(1,nSteps);
EulerTime = zeros(1,nSteps);
EulerTemp(1) = T0;
EulerTime(1) = 0;
tstart=0;
for EulerLoop = DtInt:DtInt:Tf
DT = -K*(T0-Te)*Dt;
T0 = T0+DT;
tstart = EulerLoop;
EulerTemp(1+EulerLoop/DtInt) = T0;
EulerTime(1+EulerLoop/DtInt) = tstart;
end
PercentError = abs(EulerTemp-ExactTemp)/ExactTemp;
end

Sign in to comment.

Accepted Answer

YT
YT on 1 Dec 2018
You could use round(), floor(), ceil() to first round the floats to integers and then use unique() to remove possible duplicates
Tf = 500; %constant
nSteps = 1:1:20; %steps
Dt = Tf./nSteps
unique(round(Dt))
ans =
25 26 28 29 31 33 36 38 42 45 50 56 63 71 83 100 125 167 250 500
  2 Comments
JWalker
JWalker on 1 Dec 2018
I literaly just realised that and finished coding that. Can I just check the code for that would be:
nSteps = 1:1:40
Dt = Tf/nSteps; %Makes Dt step size foe new steps
DtInt = floor(Dt); %Makes Dt Intergers
DtInt= unique(DtInt); %Makes unique intergers
madhan ravi
madhan ravi on 1 Dec 2018
Dt = Tf./nSteps;

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!