Problem removing negative numbers from my vector using for loop.

1 回表示 (過去 30 日間)
John Lutz
John Lutz 2017 年 9 月 27 日
コメント済み: OCDER 2017 年 9 月 27 日
Not sure what to exactly do from here, as nothing has helped online. Not very good with for loops. Whenever doing this code (as shown) it deletes whatever amount of negatives I have from the start, rather than deleting the actual negative numbers.
Please keep and mind I must use a for loop and input display when doing this. Thank you.
  2 件のコメント
OCDER
OCDER 2017 年 9 月 27 日
Next time, post the code instead of a screen shot - it makes it easy for us to solve your issue via copy and paste. Use the {} code button to make it neat like this:
function y = displayVector
%code here
John Lutz
John Lutz 2017 年 9 月 27 日
Ok, thank you!

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

採用された回答

OCDER
OCDER 2017 年 9 月 27 日
編集済み: OCDER 2017 年 9 月 27 日
Since you need a loop, consider creating a logical index matrix and then changing this where the negative numbers are. After the loop, delete the negative number from y.
function y = displayVector
y = input('Display vector: ');
DelIdx = zeros(1, numel(y), 'logical');
for k = 1:numel(y)
if y(k) < 0
DelIdx(k) = 1;
end
end
y(DelIdx) = [];
  2 件のコメント
John Lutz
John Lutz 2017 年 9 月 27 日
Thank you!
OCDER
OCDER 2017 年 9 月 27 日
You're Welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by