Assigning value to vector in based on conditional

8 views (last 30 days)
I'm trying to execute the following loop:
for m =1:length(r)
if imag(prelimsixmmrebar(m)) ~= 0
prelimsixmmrebar(m) = 4*2*Rc(m)*asin(r(m)/Rc(m))
end
end
with
r = 0.5:2.5;
h = (0.4/1.75)*r;
Rc = ((h.^2)+(r.^2))./(2.*h);
and
prelimsixmmrebar = ceil(180./acos((1.1^2)./(2*(r.^2)))).*2.*Rc.*asin(r./Rc)
If I understand correctly, the line inside the "if" statement should assign a new value only to the first element in prelimsixmmrebar, since this is the only one for which the boolean is true.
However, running the code reassigns new values to all of the elements. Using breakpoints to analyze, it seems that the reassignment is definitely happening at the line inside the "if" statement.
What am I missing? How would I reassign the value of the element - and only the value of that element in the vector prelimsixmmrebar?
Thanks in advance!!

Accepted Answer

Orion
Orion on 25 Nov 2014
Hi,
your code didn't reassign new value to all elements. only to the first one. it's just a "display impression".
r = 0.5:2.5;
h = (0.4/1.75)*r;
Rc = ((h.^2)+(r.^2))./(2.*h);
prelimsixmmrebar = ceil(180./acos((1.1^2)./(2*(r.^2)))).*2.*Rc.*asin(r./Rc);
disp(prelimsixmmrebar)
1.0e+02 *
0.0000 - 1.2103i 4.3138 + 0.0000i 6.3620 + 0.0000i
Actually here, there is only the first element which is complex. the 2 and 3 element have an imaginary part equal to zero. but because one of the element have an imaginary part, they are all displayed with an imaginary part (even if it's 0).
then in you're code :
for m =1:length(r)
if imag(prelimsixmmrebar(m)) ~= 0
prelimsixmmrebar(m) = 4*2*Rc(m)*asin(r(m)/Rc(m));
end
end
disp(prelimsixmmrebar)
4.1379 431.3756 636.2015
with m = 1, you check the condition and reassing a new value in your vector, but this value is a real number
so at the end, prelimsixmmrebar contains only real numbers, and there is no longer imaginary part displayed.
  1 Comment
James Scanlan
James Scanlan on 26 Nov 2014
Duh…of course. I wasn't paying attention to the scientific notation; that's what I get for trying to code late. Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!