MATLAB Answers

0

my for loop is not extracting all the possible values

Asked by Naomi Amuzie on 4 Jun 2019
Latest activity Commented on by Naomi Amuzie on 4 Jun 2019
My for loop isn't extracting all the possible values in the x variable (a 61-by-61 double). I am looping through the x values in a straight horizontal line (like increments).
The x 61-by-61 double is 61 columns of values from -3 to 3 in increments of 0.1. For example, all of column thirty-one is 0, all of column thirty-two is 0.1, all of column thirty-three is 0.2, then it goes 0.3,0.4,0.5...2.6,2.7,2.8,2.9, 3.
This is a small step in a geology project. I've tried using the numel function to loop through all the values within the x variable. Still got the same error.
I am only getting 0, 0.5, 1, 1.5, 2, 2.1, 2.2, 2.5, 2.6, 2.7, and 3, no matter what I try. These are columns, 31, 36, 41, 46, 51, 52, 53, 56, 57, 58, and 61.
I've tried doing this as well to grab the row and the columns (because technically, that's my end goal):
for idx = 0.0000:0.1000:3.0000 %each increment within the line
if find(x == idx) %if they equal and it is true, thus returning 1
[row,col] = find(x == idx); %get the row/column
disp([row,col]) %display such
end
end
However, I only get columns, 31, 36, 41, 46, 51, 52, 53, 56, 57, 58, and 61 which corresponds to the error values I am getting from before.
This is what I have so far:
% this is the straight line
xln = [0.0 3.0];
yln = [0 0];
plot (xln, yln);
xln = [0.0000 3.0000]; %the x values from the straight line
for idx = 0.0000:0.1000:3.0000 %each increment within the line
disp(x(x == idx)) %if x is the same as the index, then display it
end
or
for idx = 0.0000:0.1000:3.0000 %each increment within the line
if find(x == idx) %if they equal and it is true, thus returning 1
[row,col] = find(x == idx); %get the row/column
disp([row,col]) %display such
end
end
I am suppose to be getting ALL the values between 0 and 3 by increments of 0.1 because those all exist in the x 61x61 double.

  0 Comments

Sign in to comment.

2 Answers

Answer by Guillaume
on 4 Jun 2019
 Accepted Answer

A very important rule when you use floating point numbers on a computer (not just matlab, but all languages). Never compare floating point numbers with ==. See the FAQ: Why is 0.3 - 0.2 - 0.1 not equal to 0. A very rough summary (read the topic in details) is that many numbers (such as 0.1) cannot be stored exactly and == does exact comparison.
The way to check if two numbers are equal is by comparing their absolute difference to an arbitrary small value (amplitude of that value depending on your use case).
>> a = 0.1 + 0.1 + 0.1
a =
0.3
>> b = 0.3
b =
0.3
>> a == b %return false!
ans =
logical
0
>> abs(a-b) < 1e-12 %proper way to check for equality
ans =
logical
1
However, for what you're trying to do, ismembertol would be better as it would avoid the loop altogether
idx = 0.0000:0.1000:3.0000
matchingxindex = find(ismembertol(x, idx)); %loop not required
If you need row, col index:
[row, col] = ind2sub(size(x), matchingxindex)

  3 Comments

hi, it didn't work all the way. The columns with 0.6, 0.7, 0.8, 0.9, 1.1,1.2,1.3,1.4, and 1.5 did not show up.
idx = 0.0000:0.1000:3.0000;
matchingxindex = find(ismembertol(x, idx)); %loop not required
[row, col] = ind2sub(size(x), matchingxindex);
disp([row,col]);
I also reformed my code without the for loop, and I got the exact same error except with the values.
xln = [0.0000 3.0000]; %from before
xvalues = (0.0000:0.1000:3.0000);
lia = ismember(x,xvalues);
disp(x(lia))
Didn't include the ind2sub, changed up what I had to incorporate your advise/code and it worked! Thank you so much!

Sign in to comment.


Answer by Arvind Sathyanarayanan on 4 Jun 2019
Edited by Arvind Sathyanarayanan on 4 Jun 2019

Have you tried this:
for idx=0.0:0.1:3
if any(x(:)==idx) %if they equal and it is true, thus returning 1
[row,col] = find(x == idx) %get the row/column
disp([row,col]) %display such
end
end
From the documentation of find():
To find a noninteger value, use a tolerance value based on your data. Otherwise, the result is sometimes an empty matrix due to floating-point roundoff error.
This might be your issue

  2 Comments

hi, it is returning the exact same erroneous answer :(. I've been at it for 2 days, as simple as this is. :(
I think the problem is the ==. because when I do simply disp(x == idx), even though 0, 0.1,0.2...1.4,1.5,1.6...2.7,2.8,2.9, 3 are in x, I am only getting 0, 0.5, 1, 1.5, 2, 2.1, 2.2, 2.5, 2.6, 2.7, and 3. :(

Sign in to comment.