converting a specific string to two fractions

I'm trying to write a function that takes as input a string in the form of '12/345' and would return two integers:
numerator = 12
denominator = 345

回答 (2 件)

Wayne King
Wayne King 2011 年 10 月 23 日

0 投票

One way:
inputfrac = '12/345';
loc = find(ismember(inputfrac,'/')==1);
num = str2num(inputfrac(1:loc-1));
den = str2num(inputfrac(loc+1:end));

1 件のコメント

Walter Roberson
Walter Roberson 2011 年 10 月 23 日
The find is not needed in the above:
[tf, idx] = ismember(inputfrac,'/');
loc = idx(tf);

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

Andrei Bobrov
Andrei Bobrov 2011 年 10 月 23 日

0 投票

numden = cellmat(@str2num,regexp( '12/345' ,'/', 'split'))

1 件のコメント

Benjamin Isetta
Benjamin Isetta 2011 年 10 月 23 日
I like this one. completely different than the way I made mine.

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2011 年 10 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by