Uning Only Interger Values

1 回表示 (過去 30 日間)
JWalker
JWalker 2018 年 12 月 1 日
コメント済み: madhan ravi 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
madhan ravi
madhan ravi 2018 年 12 月 1 日
Dt = Tf./nSteps;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Preprocessing についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by