Help with indexing error

1 visualizzazione (ultimi 30 giorni)
William_M
William_M il 2 Giu 2018
Risposto: Walter Roberson il 2 Giu 2018
function B = Ecomp(e,f)
%Compares 2 elements of equal length (unfactored) in
%Sn and sees if theyre the same, i.e cyclic permutations of each other.
B='TRUE';
k=0;
n=length(e);
for i = 1:n
if f(i) == e(1)
k=i;
end
end
for j = 1:n-1
if f(mod(k+j,n)) ~= e(j+1)
B='FALSE';
return
end
end
end
This function is supposed to compare two vectors and see if they are cyclic permutations of each other. For example, Ecomp([1,2,3,4],[4,1,2,3]), should return 'TRUE', however when I input this the error that I get is:
>> Ecomp([1,2,3,4],[4,1,2,3])
Subscript indices must either be real positive integers or logicals.
Error in Ecomp (line 13)
if f(mod(k+j,n)) ~= e(j+1)
I can't see why as my indexing starts from 1, any help much appreciated.

Risposta accettata

Walter Roberson
Walter Roberson il 2 Giu 2018
mod() can return 0.

Più risposte (0)

Categorie

Scopri di più su Denoising and Compression in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by