How to make the variable pace, a data type double

3 ビュー (過去 30 日間)
B
B 2021 年 9 月 1 日
コメント済み: Stephen23 2021 年 9 月 1 日
I am trying to make a function that gives me the pace required given an athletes goal
function pace = required_pace(time,distance) % time = [hrs,mins,secs] distance = km
time=floor(time)
a=time/distance
pace=duration(a,'Format','hh:mm:ss')
end
% Calling the function
time=[0,33,3] % the altheltes goal times
distance=10 %km
required_pace(time,distance)
% i get pace=0:3:18 per km
So, i think i got the right answer but i am required to have the variable pace be a data type double and i am not sure how i am meant to write the function code to get that

採用された回答

Stephen23
Stephen23 2021 年 9 月 1 日
編集済み: Stephen23 2021 年 9 月 1 日
T = [0,33,3]; % the atheletes goal time [H,M,S]
D = 10; % km
P = required_pace1(T,D)
P = 1×3
0 3.0000 18.3000
class(P)
ans = 'double'
P = required_pace2(T,D)
P = 1×3
0 3.0000 18.3000
% Using DURATION:
function pace = required_pace1(time,distance) % time = [H,M,S], distance = km
durn = duration(time,'Format','hh:mm:ss.SSSSSSSS');
pace = sscanf(char(durn/distance),'%f:',[1,Inf]);
end
% Without DURATION:
function pace = required_pace2(time,distance) % time = [H,M,S], distance = km
secs = [60*60,60,1]*time(:);
temp = secs/distance;
pace = nan(1,3);
pace(3) = mod(temp,60); % seconds
temp = fix(temp/60);
pace(2) = mod(temp,60); % minutes
temp = fix(temp/60);
pace(1) = temp; % hours
end
  2 件のコメント
B
B 2021 年 9 月 1 日
Thanks, i am pretty new to Matlab, for future reference could you explain to me what line
pace = sscanf(char(durn/distance),'%f:',[1,Inf]);
is doing
Stephen23
Stephen23 2021 年 9 月 1 日
"for future reference could you explain to me what line ... "
sscanf(char(durn/distance),'%f:',[1,Inf]);
% ^^^^^^^^^^^^^ divide duration by distance
% ^^^^^ ^ convert duration to character
%^^^^^^ ^^^^^^^^^^^^^^ convert character to double
You can easily see the intermediate results yourself by printing them to the command window.

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

その他の回答 (1 件)

Matt J
Matt J 2021 年 9 月 1 日
pace=time/distance
  1 件のコメント
B
B 2021 年 9 月 1 日
yeah i have done that before but i get
pace=[0,3.3,0.3]
%what i need is
pace=[0,3,18.3]%[hrs,mins,sec]

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by