Writing a function for division

34 ビュー (過去 30 日間)
Ben Davis
Ben Davis 2012 年 3 月 6 日
Hi i am planning on writing a function based around division, and also include remainders. For example, 15/4= 3 remainder 3.
My code so far is:
function [q,r]=Divide(x,y)
q=x/y
q=floor(q) %so values are rounded down to allow remainder
r=x-(q*y) %my remainder value
end
q is the whole integer, and r is the remainder.
Also i want to be able to insert values for 'x' as a vector but act as an integer value.
All help would be great!
Thanks :)

回答 (1 件)

Jan
Jan 2012 年 3 月 6 日
function [q, r] = Divide(x, y)
q = x ./ y;
q = floor(q);
r = x - (q .* y);
end
The elementwise operators ./ and .* allows arrays for x and y, if they have the same size or one is a scalar.
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 3 月 7 日
Divide = @(x,y)[floor(x./y),mod(x,y)]
Divide = @(x,y)[fix(x./y),rem(x,y)]

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

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by