Decimal increments in a for loop

9 ビュー (過去 30 日間)
nth24601
nth24601 2018 年 10 月 21 日
編集済み: Walter Roberson 2018 年 10 月 24 日

Hello, I am trying to make a for loop in which i goes up by decimal increments and for each value of i (1, 1.1, 1.2, 1.3) a different value for a function is calculated and stored. I am aware that there are such threads already existing but none of them actually seem to solve the problem. My code so far looks like that:

z = 1:0.1:100;
for i=1:length(z)
  if i<10
      y(:,i) = function of i
  elseif (10<=i)&&(i<50)
      y(:,i) = another function of i
  elseif (50<=i)&&(i<=100)
      y(:,i) = another function of i
  end
end

The problem with that is that i does not actually go up by 0.1, it goes up by 1, and the function calculates values for i equal to 1,2,3,... etc I want it to take i=1, calculate the value of y for that, then take i=1.1, calculate the value for that etc.

採用された回答

Walter Roberson
Walter Roberson 2018 年 10 月 22 日
編集済み: Walter Roberson 2018 年 10 月 24 日
  1 件のコメント
nth24601
nth24601 2018 年 10 月 24 日
Thanks, the answers in this link helped me solve this problem.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 10 月 21 日
z = 1:0.1:100;
for i=1:length(z)
if z(i)<10
y(:,i) = function of i
elseif (10<=z(i))&&(z(i)<50)
y(:,i) = another function of i
elseif (50<=z(i))&&(z(i)<=100)
y(:,i) = another function of i
end
end
  3 件のコメント
nth24601
nth24601 2018 年 10 月 22 日
This gets close to the answer but it doesnt make i go by increments of 0.1, it only creates a 1000 entries for i, but i keeps going up by 1. I need the final value of i to be 100. Thank you for the answer though.
madhan ravi
madhan ravi 2018 年 10 月 22 日
You can’t index 0.1 as an index , See indexing in MathWorks Page you will understand only an integer can be used as an index not a float

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by