フィルターのクリア

how to create a vector with certain range of values

5 ビュー (過去 30 日間)
ekko ekkosen
ekko ekkosen 2015 年 5 月 13 日
回答済み: Walter Roberson 2015 年 5 月 13 日
how can i create a vector where i chose that the first n numbers in the vector is 0 and then from n+1 the values are 1 to end. example if n =3 and the vector is 10 long then it would be [0 0 0 1 1 1 1 1 1 1]. or if n = 2 than [0 0 1 1 1 1 1 1 1 1]. i will be using this on a vector that is 1X250 and is changing the n 250 times in a for loop.

採用された回答

Chad Greene
Chad Greene 2015 年 5 月 13 日
There should be no need for a loop. Enter any value for n:
n = 3;
v = zeros(1,250);
v(n+1:end) = 1;

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 5 月 13 日
[zeros(1,n),ones(1,TheLength - n)]
However if this is in a "for" loop and you are certain that the loop is being run in order, then
V = ones(1,TheLength);
for n = 1 : TheLength
V(n) = 0;
...
end
this zeros one more entry every iteration, which is less work than building a new vector

カテゴリ

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