How do I assign a variable to be any whole number?
Show older comments
I want the "if" statement to check if t is an increment of 2*pi/w, so I want j to be any whole number but I have not been able to figure out a way to make j any whole number so that the "if" statement will check if t is an increment of 2*pi/w. Also you can ignore the plot3(move points), I know this is an error and I am planning on fixing this later.
for i=1:w/2
t{i} = (i-1)*e:pi/w:(n*2*pi)+e*(i-1); %t value for CCW helices
x{i} = r*sin(t{i});
y{i} = r*cos(t{i});
z{i} = (h/(n*2*pi))*t{1};
if t{i}==j*(2*pi)/w
plot3(move points)
else
plot3(x{i},y{i},z{i}, '.','MarkerSize',25,'MarkerFaceColor','yellow','MarkerEdgeColor','yellow') %plot CCW helices
hold on;
end
hold on;
end
hold on;
2 Comments
Guillaume
on 2 Jul 2018
if t is an increment of 2*pi/w
t{i} is a vector of numbers. What does it mean for you for a vector to be an increment of something. Note that the difference between consecutive elements of t{i} is pi/w, since this is how you defined it.
Julia Morandi
on 2 Jul 2018
Answers (1)
Jan
on 2 Jul 2018
You t{i} is a vector:
t{i} = (i-1)*e : pi/w : (n*2*pi)+e*(i-1);
Therefore if t{i}==j*(2*pi)/w would only be true, if all elements of the vector are exactly j*(2*pi)/w. Due to the rounding of IEEE754 values, this is very unlikely. Please read FAQ: Why is 0.3-0.2~=0.1 (link).
Do you want to text single elements of this vector, or is really an all() wanted?
Maybe you want:
if any(t{i} - j*(2*pi)/w < 100 * eps(t{i}))
or:
index = abs(mod(t{i}, (2*pi)/w)) < 1e-7;
and then use this logical index to address certain x{i}, y{i} and z{i}.
Please explain, what you want to do exactly. It is hard to fix the code, when its intention was not mentioned.
2 Comments
Julia Morandi
on 2 Jul 2018
Not really. Note that it is usually much easier and faster to check all the elements at once rather than each one individually.
It's still not clear what you mean by increment of. Do you mean multiple of 2*pi/w? If so, why don't you create your vector as multiple of 2*pi/w to start with? e.g:
t{i} = (1:100)*2*pi/w; %all elements are guaranteed to be multiple of 2*pi/w
Also note, t is a cell array. Individual elements of the cell array, i.e. t{i}, are vectors. Individual elements of these unnamed vectors would be t{i}(j).
Categories
Find more on Entering Commands 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!