I'd like to delete 24th one of "for 1:1:30"

2 ビュー (過去 30 日間)
Jaeyoung
Jaeyoung 2012 年 1 月 29 日
Hi.
I'd like to delete 24th one of "for 1:1:30" Actually, I can run twice to simulate it as below.
for 1:1:23 and for 24:1:30
However, I'd like to do it in one loop.
I have one more question. Can you let me know a function to return number 1( or zero) when an input is negative value like -5.
a= somefuntion (-5)
ANSWER : 1 (or 0)
Thank you.
Best regards,
Jaeyoung Park
  1 件のコメント
the cyclist
the cyclist 2012 年 1 月 29 日
In the future, I suggest that if you have two questions, split them up into two separate entries.

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

採用された回答

Walter Roberson
Walter Roberson 2012 年 1 月 29 日
There are a number of different ways:
for K = 1 : 30 %#1
if K == 24; continue; end
...
end
for K = setdiff(1:30,24) %#2
...
end
KV = 1:30; KV(24) = []; %#3
for K = KV
...
end
For your second question, again there are multiple ways:
function r = somefunction(v) %#1
if v < 0
r = 1;
else
r = 0;
end
end
function r = somefunction(v) %#2
r = 0 + (v < 0);
end
somefunction = @(v) v < 0; %#3

その他の回答 (2 件)

the cyclist
the cyclist 2012 年 1 月 29 日
Not sure I understand what you mean by your first question, but for your second one:
function out = isNegative(x)
out = double(x<0); % Using "double" to convert from logical to numerical value
end
But why not just put "x<0" right in your code, rather than writing a function?

Jaeyoung
Jaeyoung 2012 年 1 月 29 日
Thank you guys.
Your comments are very helpful. Thank you.

カテゴリ

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