Subscript indices must either be real positive integers or logicals ERROR [duplicate] - matlab

This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 9 years ago.
I have this strange error 'Subscript indices must either be real positive integers or logicals' which most of the times pops up. There a few times though that it doesn't. My code is kind of huge and it has to do with calculating the voronoi diagram without using the voronoi function of MATLAB. The error occurs in one of the bellow code parts each time:
if (PossibleVoronoiPoints(m,2)-Slope(k)*PossibleVoronoiPoints(m,1)-c(k)>0)
or
if (PossibleVoronoiPoints(n,2)-Slope(k)*PossibleVoronoiPoints(n,1)-c(k)<0)
Can anyone help me understand what's going on? If you need the whole code i'll post it with comments if necessary.

This means what it means: one (or more) of the following subscripts: k, m, or n contains an invalid value. To overcome this error, you need to make sure that each subscript is valid, that it is either a positive integer or a logical (boolean) value (true or false).
If you're not properly familiar with matrix indexing in MATLAB, I suggest that you read this article or see this answer.

Related

How to circumvent the extremely small output values? [duplicate]

This question already has an answer here:
Is there any way to increase 'realmax' in MATLAB?
(1 answer)
Closed 7 years ago.
For example in Matlab:
log(exp(-200)
-200
however
log(exp(-2000))
-inf
Naturally the input to the log function is passed as zero as exp(-2000) is insignificant and the log gives -inf. How can I fix this to receive -2000 instead?
If you have the Symbolic Math Toolbox, it's possible to do this with Variable Precision Arithmetic. Use the vpa function and place your mathematical expression as input into vpa wrapped in a string:
>> vpa('log(exp(-2000))')
ans =
-2000.0
However, this will be represented in symbolic format, so it may be prudent to convert back to a numerical value after you're done. Convert this result using double once you perform the calculation:
>> double(vpa('log(exp(-2000))'))
ans =
-2000
It is often possible to rewrite formulas in a way they do not exceed the range of floating point values. In your case, rewriting it would be trivially -2000. A more "real world" example can be found in this question where rewriting successfully avoided the problem.

What is the meaning of [] in a matlab function argument? [duplicate]

This question already has an answer here:
How to use reshape in Matlab?
(1 answer)
Closed 7 years ago.
I found the following function call:
reshape(A, 1, [])
This flattens matrix A colum major. I am trying to understand the call. The function documentation says after A there should be a size vector for the reshaped matrix, but in here there is a one followed by [] instead of a two-vector. Is this a way of saying "Do whatever it takes so the matrix will have one row, I don't care what the width is"?
How come Matlab lets you exchange one argument for two like this? I tried googling around and did not find an explanation, and I want to understand what's going on here.
[] is an empty matrix. In many MATLAB built-in functions, an empty matrix is interpreted to mean "use the default argument here" or "automatically determine this value". Occasionally it is used to disambiguate two meanings of a function, as with the max function, where max(A,2) compares each element of A to 2 and returns the larger, while max(A,[],2) finds the largest element of each row.
If you read the help for reshape, you will see the following:
You can specify a single dimension size of [] to have the dimension size automatically calculated, such that the number of elements in B matches the number of elements in A. For example, if A is a 10-by-10 matrix, then reshape(A,2,2,[]) reshapes the 100 elements of A into a 2-by-2-by-25 array.

Subscript indices must either be real positive integers or logicals [duplicate]

This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 6 years ago.
iteration(it).NO=length_without_zero(CH);
But i have this error in this line of my matlab code:
Subscript indices must either be real positive integers or logicals.
In matlab, indexing is done from one. I'm guessing that iteration is a vector that you storing data in? If you type
iteration(0) = 10;
Then you will get that error message. My guess is that you come from another programming language where the value of 0 is used to refer to the first element of an array. Are you doing a loop to update the values in iteration? If so, you should do your loops with the it variable starting at 1. Like so;
for it = 1 : 10
iteration(it) = it - 1;
end

"index must be a positive integer or logical" error [duplicate]

This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 9 years ago.
When I try running a code I have in MATLAB, I get the following error:
Attempted to access labels(146.864,226.509); index must be a positive
integer or logical.
Error in abc (line 11)
l(y(i),x(i))=1;
The points are set to be chosen interactively. But, is it the points that should be an integer? I tried casting the points y(i), x(i) to int8, but didn't work. Or, maybe I'm using it wrong?
How can I solve the error above?
Thanks.
"Integer" in this context means that they should not have any decimal fraction, it does not refer to the type.
l(round(y(i)),round(x(i))=1
should work.
In addition, you may want to check that rounding does not lead to out-of-bounds value of your array l, i.e.
y = max(min(round(y),1),size(l,1);
N.B.: l is not a particularly good name for a variable.

Find the m-th smallest number in Matlab? [duplicate]

This question already has answers here:
How to find the index of the n smallest elements in a vector
(2 answers)
Closed 9 years ago.
Is there an efficient way to find the m-th smallest number in a vector of length n in Matlab? Do I have to use sort() function? Thanks and regards!
You don't need to sort the list of numbers to find the mth smallest number. The mth smallest number can be found out in linear time. i.e. if there are n elements in your array you can get a solution in O(n) time by using the selection algorithm and median of median algorithm.
The link is to the Wikipedia article,
http://en.wikipedia.org/wiki/Selection_algorithm#Linear_general_selection_algorithm_-_Median_of_Medians_algorithm
Edit 2: As Eitan pointed the first part of the answer doesn't address the question of finding the smallest m-th value but regarding the m-th element after the min value. The rest of the answer remains... +1 for Eitan's sharpness.
While sort is probably very efficient to begin with, you can try to see whether a find will be better. For example:
id=find(X>min(X),m,'first');
id(end) % is the index of the smallest m-th element in X
the function find has added functionality that lets you find the 'first' or 'last' elements that meet some criterion. For example, if you want to find the first n elements in array X less than a value y, use find(X<y,n,'first')
This operation stops as soon as the first element meeting the condition is encountered, which can result in significant time savings if the array is large and the value you find happens to be far from the end.
I'd also like to recap what #woodchips said already in this SO discussion that is somewhat relevant to your question:
The best way to speed up basic built-in algorithms such as sort is to get a faster hardware. It will speed everything else up too. MATLAB is already doing that in an efficient manner, using an optimized code internally. Saying this, maybe a GPU add-on can improve this too...
Edit:
For what it's worth, adding to Muster's comment, there is a FEX file called nth_element that is a MEX wrap of C++ that will get a solution in O(n) time for what you need. (similar to what #DDD pointed to)
As alternative solution, you may follow this way:
A = randi(100,4000,1);
A = sort(A,'ascend');
m = 5; % the 5 smallest numbers in array A
B = A(1:5);
I hope this helps.