Index exceeds the number of array elements (11)

4 ビュー (過去 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)
  1 件のコメント
Hemiru
Hemiru 2022 年 6 月 22 日
thank very much Kenneth George , it solve

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by