MATLAB Answers

find the element location

12 views (last 30 days)
Harsh Rob
Harsh Rob on 19 Aug 2019
Commented: Harsh Rob on 20 Aug 2019
I have returns marked in a column of 1*20million column. It has a specific value as -10, but I am not able to find out where it is.
I have tried using
but couldnt interpret the ouput. -
ans =
0×1 empty double column vector
When I check this matrix, its blank and does not even give any answer. Can someone guide on this?
I also wanted to plot these returns and histogram for this-
For plot-
But I just get the x and the y axis. The reason could be i have 20 million rows and thus, its not visible in the graph. Can we show these values, in this graph, rather than just showing the x and y axis as it gives a vague picture.
For histogram-
nbins = 4000; %i have calculated the appropriate bins
However, I am just getting everything at a point. I want to see the distribution and its should mostly be normally distributed or highly skewed towards right.


Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 19 Aug 2019
You have encountered ‘floating-point approximation error’. Most numbers are not exactly represented in floating-point precision (especially if they are the result of computations), so testing for them exactly is not usually productive. See the documentation on Floating-Point Numbers for an extended discussion.
Taking that into account, finding the indices of the ‘-10’ elements is relatively straightforward.
For example:
Returns = (0.5-rand(1, 1E+4))*30; % Test Vector
Lv = ismembertol(Returns, -10, 0.01); % Logical Vector Result
Indices = find(Lv); % Index Positions
Use your vector instead of my test vector. Experiment with the tolerance value (here 0.01) to get the result you want.
See if plotting with a marker (instead of a line) will make your data more visible. I cannot offer you any advice with respect to the histogram. We do not have your data or know what result you want, so you will need to experiment.


Sign in to comment.

More Answers (1)

Guillaume on 19 Aug 2019
If find returns empty, then we can safely say that exact -10 is not in that first column. Perhaps the number you're looking for is very close to 10 but not exact 10 (e.g. it could be -10.0000000000000017764). To find the nearest number to -10:
[~, loc] = min(abs(returns(:, 1) + 10)
diff_from_10 = returns(loc, 1) + 10
As for the rest, I don't understand what you're saying.
Note that hist has long been deprecated. histogram is the recommended function now.

  1 Comment

Harsh Rob
Harsh Rob on 20 Aug 2019
Thanks Guillaume

Sign in to comment.

Sign in to answer this question.