How can i programe this function without rem or mod?

the matlab programe ( function, one loop or two loops ) needs to solve for the quotient remainder theorem a = dq + r
where the programe takes two integers as inputs the dividend a, and the divisor d, processes them, and calculates the quotient q and the remainder 'r' as defined by the division algorithm. q, and r need to be two formal outputs of this program forany given pair, a, and d.
The program MUST NOT use mod, rem, floor, ceil, round, fix.

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 10 月 28 日

0 投票

a = 1 : 25
a = 1×25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
d = randi([2 15])
d = 6
t = int32(a) - int32(a) / int32(d) * int32(d);
t(t < 0) = t(t<0) + d
t = 1×25
1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1
int32(mod(a, d))
ans = 1×25
1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1

2 件のコメント

haider khan
haider khan 2021 年 10 月 28 日
i need to write a matlab programe, uing a function, without using mod.
Walter Roberson
Walter Roberson 2021 年 10 月 28 日
In the above, the call to mod() I used was to cross-check the output in t in order to show that the calculation I did produces the same output as mod() -- and thus that mod() is not needed.
The work is done in the two assignments to t . You can convert that work into a function.

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

カテゴリ

製品

質問済み:

2021 年 10 月 28 日

コメント済み:

2021 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by