see, i have this error in my code saying: ??? Attempted to access x(0); index must be a positive integer or logical. Error in ==> abc at 14 x(i)=mod(g^x(i-1), p);

4 ビュー (過去 30 日間)
clear all; close all; clc;
p = input('Introduceti numarul prim p= ');
g = input('Introduceti numarul prim g= ');
Nr=p-1;
x=zeros(1, Nr);
for i=1:Nr
x(i)=mod(g^x(i-1), p);
end;
disp('Rezultatul este: ');
disp(x);

採用された回答

Brendan Hamm
Brendan Hamm 2015 年 7 月 16 日
You start the loop at i=1, but then try and index x(i-1), which is the same as x(0). MATLAB starts indexes at 1 (Mathematical indexing, not programmer indexing).
That is
x = [1 2 3 4];
x(1)
ans =
1
x(0)
Subscript indices must either be real
positive integers or logicals.
  1 件のコメント
Andreea Alexandra
Andreea Alexandra 2015 年 7 月 16 日
編集済み: Andreea Alexandra 2015 年 7 月 16 日
ok, I got it, thank you so much. how could I solve it? the thing is, that is the range this alg gives me in it's theory..

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 7 月 16 日
You have x(i)=mod(g^x(i-1), p); . When "i" is 1, that is mod(g^x(1-1),p) which is mod(g^x(0),p) . However there is no element #0 of a vector.
You need to think more about your initial conditions, of what is to happen on the very first iteration.
  2 件のコメント
Andreea Alexandra
Andreea Alexandra 2015 年 7 月 16 日
Thank you very much. I understand now. I'll think more about it.
Walter Roberson
Walter Roberson 2015 年 7 月 16 日
I suspect what you want is
x(1) = 1;
for i=2:Nr
x(i)=mod(g^x(i-1), p);
end

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by