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

1 回表示 (過去 30 日間)
Julen Vicente Pipaon
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);
  1 件のコメント
Julen Vicente Pipaon
Julen Vicente Pipaon 2021 年 2 月 21 日
Thank you so much for your answer.

サインインしてコメントする。


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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by