Index exceeds the number of array elements (11)

1 次查看(过去 30 天)
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 个评论
Kenneth George
Kenneth George 2022-6-22
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)

请先登录,再进行评论。

回答(1 个)

Kenneth George
Kenneth George 2022-6-22
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)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by