I have a vector of 60,000 x 1, each part containing the values 0-9
What do I need to do to find out in which cells, for example, the value is 1?
The goal is to have an index of cells equal to 1
Best Answer
Even though OP says "cells", based on their response to my comment, it seems that they were talking about simple vectors after all. In which case the following code:
myVector = randi([0,9],60e3,1); % vector of 60,000x1 containing integers between 0 and 9
linearIdx = find(myVector == 1); % linear indices of elements equal to 1
will work (in the second line, 1 can be substituted for whatever number is required).
It would be a good idea to look at ind2sub if you plan to also use matrices (instead of just vectors) some day.
Also, most applications support logical indexing, which is faster and uses less memory than linear indexing, so maybe look that up too.
This behavior is due to precision limitations of floating-point arithmetic. When specifying an interval [a,b] in steps of d using the colon operator (a:d:b), MATLAB generates the first half of the interval using a+xd and the latter half using b-xd, where x is a natural number.
As a result, there is a greater accumulation of rounding errors toward the middle of the generated vector. Any subsequent arithmetic/relational operations on the vector may produce unexpected results because of these rounding errors.
Use best practices when writing code to perform such operations on floating point numbers. Suggestions can be found in this relatedMATLAB Answers post.
Best Answer