# Vector. Change the way my program gives me the answer.

1 view (last 30 days)
Julen Vicente Pipaon on 21 Feb 2021
This program gives me the answer like this : c =
-3
c =
4
But I want the answer like this: c =
-3 4
The program:
v = [1 2 -3 4];
n = 1;
l = length (v);
while (n < l)
a = v(n);
b = v(n+1);
n = n+1;
if (a < 0 && b > 0)||(a > 0 && b < 0)
c = [b]
end
end

Walter Roberson on 21 Feb 2021
v = [1 2 -3 4];
n = 1;
l = length (v);
while (n < l)
a = v(n);
b = v(n+1);
if (a < 0 && b > 0)||(a > 0 && b < 0)
c(n) = b;
else
c(n) = nan;
end
n = n+1;
end
c
c = 1×3
NaN -3 4
##### 1 CommentShowHide None
Julen Vicente Pipaon on 21 Feb 2021
Thank you so much

### More Answers (2)

Paul Hoffrichter on 21 Feb 2021
v = [1 2 -3 4];
n = 1;
l = length (v);
ii = 1;
c = [];
while (n < l)
a = v(n);
b = v(n+1);
n = n+1;
% if (a < 0 && b > 0)||(a > 0 && b < 0)
if sign(a) ~= sign(b)
c(ii) = [b];
ii = ii + 1;
end
end
disp(c);
##### 1 CommentShowHide None
Julen Vicente Pipaon on 21 Feb 2021
Thank you so much for your answer.

Paul Hoffrichter on 21 Feb 2021
v = [1 2 -3 4];
v( diff( sign( [v(1) v] ) ) ~= 0 )
ans =
-3 4