Index of variable in a loop

1 view (last 30 days)
NGOC TAM LAM
NGOC TAM LAM on 14 Oct 2019
Edited: NGOC TAM LAM on 14 Oct 2019
Hello all,
As the short program below, when index i increase from 1 to size of coordExplore,
the coordExplore(i) does not indicate the coordExplore(rightPoint) or coordExplore(x1,y1,z1),
however, it shows only coordExplore(x1);
In the for loop, with index i, how can matlab understand coordExplore(1) = coordExplore(rightPoint) = (x1,y1,z1)
& coordExplore(2) = coordExplore(leftPoint) = (x2,y2,z2)?
%from initial 3D point (x y z) using treeExploreation fuction to create two new3D points with coordinates (x1,y1,z1) and (x2,y2,z2)
%then from these new point, using this function two create new four 3D_Points...
%x y z are scala
rightPoint = [x1,y1,z1];
leftPoint = [x2,y2,z2];
targetPoint = [x_t, y_t, z_t];
coordExplore = [rightPoint,leftPoint];
%store twop 3D points into coordExplore
temp = [];
flag = 1;
while (flag)
for i=1:length(coordExplore)
[newrightPoint,newleftPoint]=treeExploration(coordExplore(i)); %help me at here !!!!
%this function with input (x,y,z) output two new coordinates
if (newrightPoint==targetPoint) || (newleftPoint==targetPoint)
%this condition is to compare the output coordinates whether the same as the targetPoint
flag=0;
break;
else
temp =[newrightPoint,newleftPoint];
end
end
coordExplore = temp;
temp = [];
end
  4 Comments
NGOC TAM LAM
NGOC TAM LAM on 14 Oct 2019
Edited: NGOC TAM LAM on 14 Oct 2019
Hi @Looky,
When I changed to length() the issue is till there.
For example,
with my above program,
disp(coordExplore) -> x1 y1 z1 x2 y2 z2
with i=1 in the loop, disp(coordExplore(i)) -> x1. It does not display (x1, y1, z1) as I mentioned in the question that I need to get the value of the rightPoint with coordinate (x1, y1, z1).
How can I do with i=1 in the loop, coordExplore has value of 1st point of rightPoint (x1 y1 z1) then i=2, coordExplore has 2nd value of leftPoint (x2 y2 z2)?
Please help!!!
Guillaume
Guillaume on 14 Oct 2019
First, whenever you write code, and particularly if you're going to ask questions on it, write comments explaining what the code is meant to do. It's difficult to help you if we don't have an overview of what you're trying to do.
As pointed out by Looky,
for i=1:size(coordExplore)
is already a problem. size always returns a vector and the colon operator will ignore all elements but the first. Since coordExplore is a row vector, the first element of size is always 1, so that loop is equivalent to
for i = 1:1
Then, it's unclear what you're trying to do with that loop and whether or not that loop is even needed. We don't know what treeExploration is, it could be a matrix, but more likely it's a function but what does it return? What is the size of the outputs?.
It would make sense that newRightPoint is a vector, similar to RightPoint, but in that case, you'll get an error with the || in your loop (it doesn't work with vectors), so maybe it's a scalar.
And if NewRightPoint is a vector, == is probably the wrong way to compare it to another vector.
And also, as pointed out by Fabio, exact comparison of floating point values is a dangerous thing to do. See for example that:
>> (0.1 + 0.1 + 0.1) == 0.3
ans =
logical
0
is false. To learn more about this do a search on floating point comparison.
Anyway, we need a lot more information to answer your question.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!