my for loop is not extracting all the possible values
Show older comments
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.
Accepted Answer
More Answers (1)
Arvind Sathyanarayanan
on 4 Jun 2019
Edited: 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
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!