Index exceeds the number of array elements (11)

1 view (last 30 days)
Hemiru
Hemiru on 22 Jun 2022
Commented: Hemiru on 22 Jun 2022
Hello !!
label = [7 11 15 10 4 16 17 18 8 12 9 19 5 13 20 14 6 1 2 3 21 22 23 24 25 26 27 28 29 30]
n = 30;
Color = 11;
pewar = zeros(nCUE,1);
B = randperm(numel(Color));
B = [2 1 4 5 7 11 6 8 3 9 10]
for i = 1 : size(label,1)
if i <= size(B,2)
pewar(i) = B(label(i,1));
else
pewar(i) =0;
end
end
Index exceeds the number of array elements (11).
Error in color_coba (line 323)
pewarCUE(i) = B(labelCUE(i,1));
how to fix that problem.
label should have a value of 1 2 3 and in the order above it and will be assigned a random value B with a range of 11. if the value of B is run out it will be given a value of 0. but the position of the label doesn't change
  1 Comment
Kenneth George
Kenneth George on 22 Jun 2022
B = randperm(numel(Color)); seems to be an error. Since Color is a scalar, numel(Color) = numel(11) = 1. So B=1.
I believe you want:
B = randperm(1:Color)

Sign in to comment.

Answers (1)

Kenneth George
Kenneth George on 22 Jun 2022
Think about what happens on the 6th loop, i=6, label(i,1) = 16. Since i=6 <= 11, the if statement returns true.
then, B(label(i,1)) = B(16). What should B(16) return, if B only has 11 elements?
Instead I think your if statement needs to be:
if label(i) <= size(B,2)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!