Index exceeds the number of array elements.
Show older comments
Hello everyone. I have a problem with error "Index exceeds the number of array elements. Index must not exceed 10.". I understand why theres an error but dont know how to work around it. The error message shows up because Iam comparing a value to nothing, but I need to do something with the last of the duplicates, which in this case is the last value of the array. I tried to explain the program and what it needs to do in each row.
x = [25 10 10 12 10 25 13 25 25 11]'; %input
x = sort(x,'descend'); %sorting from largest to smallest
x_pocet=numel(x); %number of values
dupli = grouptransform(x,x,@numel); %finding duplicates
for i=1:x_pocet %for i from 1 to number of values
if dupli(i)<=1 %if the value isnt a duplicate calculate profit
trzba = i*(x(i));
fprintf('\n Pro cenu %d. je trzba %d. \n',x(i),trzba)
else if dupli(i)>=1 && x(i)==x(i+1) %if the value is a duplicate and the value is same as the next one do nothing
else %if the value is a duplicate but is the last of the duplicates calculate profit
trzba = i*(x(i));
fprintf('\n Pro cenu %d. je trzba %d. \n',x(i),trzba)
end
end
end
If anyone could help me with solving the problem, it would be much appriciated.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing 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!