How do you make a program read a function for a script and creating a function?

22 ビュー (過去 30 日間)
Kenny Dang
Kenny Dang 2017 年 1 月 28 日
編集済み: Guillaume 2017 年 1 月 30 日
I am very new to matlab. So sorry if I am a noob at programming simple things like this. What I am trying to do is, insert a vector and if the vector is a negative number turn it into a 0 and keep the rest of the positive number the same. so for this code it should pop out [12 0 4 0] but all I get is 12.
  2 件のコメント
Kenny Dang
Kenny Dang 2017 年 1 月 28 日
I have to use for and if statements
Jan
Jan 2017 年 1 月 28 日
@Kenny: Please post the code as text in following questions. Screenshots do not allow to copy&paste. Thanks.

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

採用された回答

Jan
Jan 2017 年 1 月 28 日
編集済み: Jan 2017 年 1 月 28 日
Omit the first loop, because it does not perform anything.
for n = 1:1:x(i)
should be fixed to:
for i = 1:length(x)
to create a loop over all elements of the vector x.
The rest of your loop is fine, when you omit the return. Set a semicolon behind "y(i) = x(i)" to suppress the output.
  4 件のコメント
Jan
Jan 2017 年 1 月 30 日
編集済み: Jan 2017 年 1 月 30 日
@Kenny: Perhaps you mean theoutput to the command window caused by the forgotton semicolon after "y(i)=x(i)"? Avoid global variables. They cause more problems than they solve. A proper indentation helps to see the structure of the code:
for i = 1:length(x)
if x(i) > 0
y(i) = x(i);
else
y(i) = 0;
end
end
Guillaume
Guillaume 2017 年 1 月 30 日
編集済み: Guillaume 2017 年 1 月 30 日
As Jan says, avoid global. There's absolutely no reason to have global x. While we're at it, you should also preallocate y before the loop (otherwise it gets resized at each loop iteration, slowing the code down), so:
y = zeros(size(x)); %make y the same size and shape as x
And I would also replace length by numel, so that the loop works whether x is a vector or a matrix:
for i = 1:numel(x) %numel is a lot safer than length

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2017 年 1 月 28 日
Get rid of lines 2-13 and have this:
x(x<0) = 0;
  2 件のコメント
Kenny Dang
Kenny Dang 2017 年 1 月 28 日
Forgot to say I have to use if and for statements
Image Analyst
Image Analyst 2017 年 1 月 28 日
And you forgot to tag it as homework. I've done that for you. Get rid of lines 3 and 4 and make line 5 like Jan has. You might also initialize y before the loop
y = x;
and change lines 6-11 to this
if y(i) < 0
y(i) = 0
end
You certainly don't want line 8 in there - why return from your function while you're looping????

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


Guillaume
Guillaume 2017 年 1 月 28 日
Your code is very puzzling. You have for loop using i that does nothing:
for i = 1:j
end %there's nothing in the body of the loop!
At the end of this loop, i is equal to j (what meaningless variable names!). Then you have an n loop that goes from 1 to the value of x(i), which has established is x(j), and thus -5. Hence that loop will not even execute. Even if it did start, there's nothing in the loop that depends on n.
I really don't understand what you were trying to do with these loop. In any case, no loop is needed:
x = [12 -3 4 -5];
y = x;
y(y<0) = 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