How to insert fractional values in for loop?

9 ビュー (過去 30 日間)
Athira T Das
Athira T Das 2022 年 7 月 16 日
回答済み: Harshit Gupta 2022 年 7 月 16 日
Here the values of s1 can be 0,1/2,1,3/2
But in my for loop the values of s1 is integer, how to give the fractional values to s1
clc; clear all; close all;
M=3
for m1 =0:M
for s1=0:m1/2
X=2^(s1)
end
end
X

回答 (1 件)

Harshit Gupta
Harshit Gupta 2022 年 7 月 16 日
Hi, you just need to add in a step size like this:
for i = 1:0.5:3 % Here, step size is 0.5
i
end
i = 1
i = 1.5000
i = 2
i = 2.5000
i = 3
You wouldn't need to create two loops if you're using the step size:
clc; clear all; close all;
M=3;
for m1 =0:0.5:M
X = 2^(m1)
end
X = 1
X = 1.4142
X = 2
X = 2.8284
X = 4
X = 5.6569
X = 8
X
X = 8
You should spend some time understanding the fundamentals of MATLAB
Documentation: MATLAB for
Hope this helps!

カテゴリ

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