Array element containing infinity

Hi. I have a problem in array indexing containing infinity.
I have to compute the function f(a)=1-1/a mod 13 . It takes values from [infinity 0,1,2 ...12] and out put should be [1,inf,0,7,5,4,6,3,12,9,11,10,8 and 2]
I have tried to code it but it gives error at infinity.
Y_trans=[];
p=[];
n=13;
P_2=[];
y_axis=[];
for i=1:13
if modInv(i,n)==0
P_2(i)=Inf
else
P_2(i)=modInv(i,n);
[Y_trans(i)]= mod(1-P_2(i),n);
end
end

6 comentarios

madhan ravi
madhan ravi el 17 de Nov. de 2020
Your question and function seems not the same to me atleast , 1 - inf is -inf why are you saying inf?
lilly lord
lilly lord el 17 de Nov. de 2020
f(a)=1-1/a
f(inf)=1-0=1
f(0)=1-inf=inf
f(1)=1-1=0
and so on
The expression "p mod q" in matlab is mod(p,q) but it's not clear what p should be in your formula,
f(a)=1-1/a mod 13
The mod operator is typically give then same precedence as multiplication and division so one interpretation is
f(a) = 1-mod(1/a, 13)
but other interpretations are
f(a) = mod(1-1/a, 13)
f(a) = 1-1/mod(a, 13)
None of these interpretation are in your code, though. You're using the following which ignore the reciprocal "1/" unles that's already done in modInv (this is why it's helpful to define your variables so we understand what they are).
f(a) = mod(1-a, 13)
Nevertheless, an inf value shouldn't cause an error in the mod function so please share the entire error message and provide us with your inputs.
p=13.
modInv calculates 1/a mod p
function xInv = modInv(x,n)
% ModInv(x,n) computes the multiplicative inverse of x modulo n if one
% exists; errors if no such inverse exists
if gcd(x,n) ~= 1
%error('x has no inverse modulo n')
xInv=Inf;
return
end
[d, a, b] = gcd(x,n);
xInv = mod(a,n);
end
lilly lord
lilly lord el 17 de Nov. de 2020
I want the out put as [1,inf,0,7,5,4,6,3,12,9,11,10,8 and 2]
When I start i from 0 to 12, it gives an error
"Array indices must be positive integers or logical values".
Adam Danz
Adam Danz el 17 de Nov. de 2020
" please share the entire error message", all of it, and let us know which line is throwing the error.
The only indices I see are i and n and both are positive integers.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Etiquetas

Preguntada:

el 17 de Nov. de 2020

Comentada:

el 17 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by