Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
mod function

Subject: mod function

From: pluton schmidt

Date: 13 Mar, 2011 19:18:04

Message: 1 of 7

Hi all,

in matlab, is there a function modb similar to mod that works as follows:

X and Y are positive integers :
modb(Y,X) is Y if Y<X
modb(X,X) is X

thanks

Subject: mod function

From: Bruno Luong

Date: 13 Mar, 2011 19:44:05

Message: 2 of 7

"pluton schmidt" wrote in message <ilj59c$src$1@fred.mathworks.com>...
> Hi all,
>
> in matlab, is there a function modb similar to mod that works as follows:
>
> X and Y are positive integers :
> modb(Y,X) is Y if Y<X
> modb(X,X) is X

min(X,Y)

% Bruno

Subject: mod function

From: Sadik

Date: 13 Mar, 2011 19:45:04

Message: 3 of 7

Hi Pluton,

How about min?

min([X,Y])
min([X,X])

work just as you wanted, right?

Subject: mod function

From: Roger Stafford

Date: 13 Mar, 2011 21:45:04

Message: 4 of 7

"pluton schmidt" wrote in message <ilj59c$src$1@fred.mathworks.com>...
> Hi all,
>
> in matlab, is there a function modb similar to mod that works as follows:
>
> X and Y are positive integers :
> modb(Y,X) is Y if Y<X
> modb(X,X) is X
>
> thanks
- - - - - - - - -
 If you mean equivalent to

 mod(Y-1,X)+1

then I don't think there is such a function in matlab, so you might have to settle for the above.

Roger Stafford

Subject: mod function

From: John D'Errico

Date: 13 Mar, 2011 22:09:05

Message: 5 of 7

"pluton schmidt" wrote in message <ilj59c$src$1@fred.mathworks.com>...
> Hi all,
>
> in matlab, is there a function modb similar to mod that works as follows:
>
> X and Y are positive integers :
> modb(Y,X) is Y if Y<X
> modb(X,X) is X

I've had occasional use for this, so I wrote clockmod.

HTH,
John

function m = clockmod(n,k)
% clockmod - clock style mod arithmetic, mod(n,k) when that is non-zero, but k when it would be zero otherwise
% usage: m = clockmod(n,k)
%
% arguments: (input)
% n,k - standard arguments to mod(n,k)
%
% m - array of the same size and shape that
% mod(n,k) would generate, but when that
% result would have been zero, it contains k.

% just the mod, most of the time
m = mod(n,k);

% but when that result would be zero, return k instead.
ind = (m == 0);
if any(ind(:))
  if numel(k) == 1
    m(ind) = k;
  else
    m(ind) = k(ind);
  end
end

Subject: mod function

From: pluton schmidt

Date: 14 Mar, 2011 12:45:08

Message: 6 of 7

Hi all,

yes, "min" should do the trick. I was so into the "mod" thing that I totally missed it! Thank you. I'll have a look at clockmod as well.

Subject: mod function

From: Roger Stafford

Date: 14 Mar, 2011 18:27:04

Message: 7 of 7

"pluton schmidt" wrote in message <ill2kj$1bh$1@fred.mathworks.com>...
> yes, "min" should do the trick. I was so into the "mod" thing that I totally missed it! Thank you. I'll have a look at clockmod as well.
- - - - - - - - -
  Using the 'min' function as opposed to a mod-like action depends very much on what you wish to happen with modb(Y,X) if Y>X. You never did state your preference in this case.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us