Index exceeds the number of array elements (11)

조회 수: 1 (최근 30일)
Hemiru
Hemiru 2022년 6월 22일
댓글: Hemiru 2022년 6월 22일
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