Uning Only Interger Values

2 次查看(过去 30 天)
JWalker
JWalker 2018-12-1
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 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2018-12-1
Can you explain more with array and index example?
JWalker
JWalker 2018-12-1
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

请先登录,再进行评论。

采纳的回答

YT
YT 2018-12-1
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 个评论
JWalker
JWalker 2018-12-1
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by