Help with indexing error

1 vue (au cours des 30 derniers jours)
William_M
William_M le 2 Juin 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Juin 2018
mod() can return 0.

Plus de réponses (0)

Catégories

En savoir plus sur Denoising and Compression dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by