Error using ==> mpower At least one operand must be scalar in mod operation

4 views (last 30 days)
hi all, i need ur little help here. i use this code
d=18;
n=19;
x=3;
d1=dec2bin(d);
r=1;
for i=1:length(d1)
r=mod((r^r),n);
if (d1(i)>=1)
r=r*(mod(x,n));
end
end
it's return this error: ??? Error using ==> mpower At least one operand must be scalar.
Error in ==> geje at 26
r=mod((r^r),n);
i don't know where is my mistake?? hope your solution

Answers (4)

Paulo Silva
Paulo Silva on 26 Jun 2011
That code is working without any error and there's no line 26 in it

zikrullah muhammad
zikrullah muhammad on 26 Jun 2011
i'am sorry it's should be line 7.
but in my matlab it's return error.
any idea why my code return: error using ==> mpower At least one operand must be scalar. ?
what the meaning of this error?
  2 Comments
Paulo Silva
Paulo Silva on 26 Jun 2011
Run the code like this and see what appears on the command line
d=18;
n=19;
x=3;
d1=dec2bin(d);
r=1;
for i=1:length(d1)
r
n
r=mod((r^r),n)
if (d1(i)>=1)
r=r*(mod(x,n));
end
end
zikrullah muhammad
zikrullah muhammad on 27 Jun 2011
i give up with this code so i try another way and it's work.
thank you for your support

Sign in to comment.


Matt Fig
Matt Fig on 26 Jun 2011
There is more going on here than you say, because the code you pasted could not possibly have that error. The variable 'r' is a scalar going into the loop. Since 'x' and 'n' (both scalars) don't change in the loop, mod(x,n) is always 3. So the only way 'r' is changed, besides the line in question, is possibly by a factor of 3. A scalar multiplied by 3 is always a scalar, and so 'r' has no chance of not being a scalar. Since both operands passed to MPOWER are scalar, there can be no error.
Thus the code you show couldn't possibly have this error, assuming you haven't masked any built-in functions. So what have you left out of your post?
And by the way, please don't post new 'answers' when you are responding to an answer given by someone else. Instead, put a comment on their answer by clicking the "Comment on this Answer" link.
  1 Comment
zikrullah muhammad
zikrullah muhammad on 27 Jun 2011
i'm sorry to post new 'answers' .
and it solve i think, i give up with this code so i try another way and it's work.
thank you for your support

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Jun 2011
r^r is an error if r is not a scalar.
MATLAB knows how to raise a square matrix to a scalar power, and knows how to raise a scalar to a matrix power, but MATLAB cannot raise a matrix to a matrix power.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!