I'm trying to take a vector as an input for a function and change all the negative values of the vector into 0's, then output the new vector.
function newX = valu(x)
for x
if (x<0)
x = x .* 2;
end
newX = x;
end
end
How would I tell Mathlab that X is a vector that will be assigned by the user (when calling the function)?

2 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 2 日
Please don’t close question which has answer.
Rena Berman
Rena Berman 2018 年 11 月 7 日
(Answers Dev) Restored edit

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

 採用された回答

Star Strider
Star Strider 2018 年 1 月 27 日

0 投票

There are much more straightforward ways to do what you want. However, to do it in a loop, you need to index the individual elements of ‘x’, then test, and if necessary replace, the elements according to the criteria you choose. (Your current loop multiplies them by 2 rather than setting them to 0. I leave that for you to define.)
Example —
for k = 1:numel(x)
if (x(k)<0)
x(k) = x(k) .* 2;
end
newX(k) = x(k);
end
Experiment to get the result you want.

1 件のコメント

Star Strider
Star Strider 2018 年 1 月 27 日
As always, my pleasure!
The (k) is a subscript. It refers to individual elements of ‘x’. See the documentation on Matrix Indexing (link) for a comprehensive discussion.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Get Started with MATLAB についてさらに検索

質問済み:

2018 年 1 月 27 日

コメント済み:

2018 年 11 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by