Combine If statement and for loop?!

Hello everybody,
I would like to combine the for loop with an if Statement:
if x<0
for i=1:length(gradient);
delta_x_{i} = x(i)-sqrt(r^2/(1+gradient(i).^2));
delta_x=[cell2mat(delta_x_)]';
delta_y_{i} = gradient(i)*delta_x(i);
delta_y=[cell2mat(delta_y_)]';
end
else
for j=1:length(gradient);
delta_x_{j} = x(j)+sqrt(r^2/(1+gradient(j).^2));
delta_x=[cell2mat(delta_x_)]';
delta_y_{j} = gradient(j)*delta_x(j);
delta_y=[cell2mat(delta_y_)]';
end
end
But as far as I can see, it is not working? What am I doing wrong? I guess I'm not seeing the wood for the trees...
Appreciate any Help! Christian

4 Commenti

Stephen23
Stephen23 il 2 Mar 2017
Modificato: Stephen23 il 2 Mar 2017
@Christian: What does "But as far as I can see, it is not working" actually mean? What do you expect that code to do? I might expect this code to do quite different things to what you expect, but unless you tell us, how do we know what you expect it to do?
Ok, sorry for these incomplete Information.
I want the code to do the following:
I have coordinates x and y. Now I would like to calculate new coordinates -> x_new=x+Delta_x. And I need to do this for x>0 and x<0 to get the coordinates I want. So I tried to solve this problem by the code above. But the result is, that I only get new coordinates which are calculated like in the "else" part.
Does this make my plans clear?
Stephen23
Stephen23 il 2 Mar 2017
Modificato: Stephen23 il 2 Mar 2017
Hmm... not totally clear to me, but someone may be able to make sense of it. Perhaps you should skip the if altogether and use logical indexing:
I got it!!!
for i=1:length(gradient1);
if x(i) > 0
delta_x(i) = x(i)+sqrt(r^2/(1+gradient1(i).^2));
delta_y(i) = gradient1(i)*delta_x_(i);
end
if x(i) < 0
delta_x_(i) = x(i)-sqrt(r^2/(1+gradient1(i).^2));
delta_y_(i) = gradient1(i)*delta_x_(i);
end
end

Accedi per commentare.

Risposte (2)

Andrei Bobrov
Andrei Bobrov il 2 Mar 2017
Modificato: Andrei Bobrov il 2 Mar 2017
delta_x = x(:)+sign(x(:)).*sqrt(r^2./(1+gradient1(:).^2));
delta_y = gradient1(:).*delta_x;
We use gradient1 instead gradient. gradient - it's function from MATLAB

1 Commento

the "Gradient" is just an example. in my script I use german words :)

Accedi per commentare.

x is a vector. From the documentation for the if keyword, "if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false."
So the body of your if statement:
if x<0
is only executed if ALL the elements of x are less than 0. Otherwise you drop into the else section.
Your corrected code is closer, using a logical scalar as the if expression, but you may be missing a couple cases. If you've preallocated delta_x and delta_y, having 0 as the last element in x won't leave those two arrays shorter than you expect. You may also want to consider what happens if x is Not-a-Number, better known as NaN.

1 Commento

Thank you for your advice. I deleted all NaN right before the for loop and now it seems to work. At least the code does what I want it to do :)

Accedi per commentare.

Richiesto:

il 2 Mar 2017

Commentato:

il 2 Mar 2017

Community Treasure Hunt

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

Start Hunting!

Translated by