sub2ind -> out of range subscript

my code is give out of range subscript error how can i solve it? D=6000x1 double
d = zeros(2, 1);
d(sub2ind(size(d), D(k), 1)) = 1;

 採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 14 日
編集済み: Walter Roberson 2020 年 6 月 14 日

0 投票

Your d is 2 x 1, so size(d) is [2 1] . When you use sub2ind(), the data values you provide must be within the size you provide in the second parameter.
Do not use sub2ind() to try to expand a matrix (though you can get away with it by lying about the size of the current last dimension, providing you are expanding on the last dimension.)
Since your second index is always 1 and the data from D is being used as the row number, you do not need sub2ind at all. Just use
d(D,1) = 1;

4 件のコメント

D G
D G 2020 年 6 月 14 日
when i use d(D(k),1) = 1; i get this error "Index in position 1 is invalid. Array indices must be positive integers or logical values." i need [2 1] to use d for Backpropagation
Walter Roberson
Walter Roberson 2020 年 6 月 14 日
Some of your D values are not positive integers. For example they might be 0.
Walter Roberson
Walter Roberson 2020 年 6 月 14 日
Question: is what you are trying to do this:
  • if any entry in D is 0, set d(1) to 1
  • if any entry in D is 1, set d(2) to 1
?
D G
D G 2020 年 6 月 14 日
My data is like this 010111001100111.... when i edited my data the problem was solved. Thanks.

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

その他の回答 (0 件)

カテゴリ

製品

質問済み:

D G
2020 年 6 月 14 日

コメント済み:

D G
2020 年 6 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by