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

조회 수: 1 (최근 30일)
Julen Vicente Pipaon
Julen Vicente Pipaon 2021년 2월 21일
댓글: Julen Vicente Pipaon 2021년 2월 21일
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
Walter Roberson 2021년 2월 21일
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

추가 답변 (2개)

Paul Hoffrichter
Paul Hoffrichter 2021년 2월 21일
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);

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by