my code is give out of range subscript error how can i solve it? D=6000×1 double
d = zeros(2, 1);
d(sub2ind(size(d), D(k), 1)) = 1;
Best Answer
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
Please inspect the values of r1, index1 and i. Most likely "size(I(index1))" does not match the needed column vector size.
We do not have magic tools to solve this problem. Everything we can do to solve the problem, can be done by yourself also. But you have additional knowledge about the values of all variables.
first calls upon the built in MATLAB functionmax to find the maximum of d. It then redefines the namemax to be associated with a variable with that value, blocking the use ofmax as a function. The next time you attempt to take max() of anything, max(d) would be interpreted as an attempt to index the variable namedmax with the index given in d, which can only work ifd is the valuetrue orfalse or is something numeric that consists entirely of 1's.
Best Answer