How can i use linspace with different intervals?

The value for x is 0 to 1. I want to use 0 to 0.9 with interval 0.01 and 0.9 to 1 with interval 0.99.
I used linspcae like this but this is giving me an error.
x = linspace(0,0.9,91;0.9,1,91);
How can i use different interval in linspace? Any idea?

 採用された回答

Rik
Rik 2020 年 5 月 8 日

0 投票

You will have to call linspace multiple times:
part1=linspace(0,0.9,91);
part2=linspace(0.9,1,91);
x = [part1,part2(2:end)];

1 件のコメント

Mahrosh
Mahrosh 2020 年 5 月 8 日
編集済み: Mahrosh 2020 年 5 月 8 日
thanks for your comments.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 5 月 8 日

0 投票

If you know both endpoints and the interval, linspace isn't the best tool for the job. The colon operator (:) is.
x = 0:0.1:0.9;
Rik's suggestion of creating each piece independently and combining them afterwards, but use colon instead of linspace.

5 件のコメント

Rik
Rik 2020 年 5 月 8 日
I had the same thought, but the interval sizes in the post itself didn't make sense to me. In hindsight I should have at least given it a mention, so thanks for the addition.
Mahrosh
Mahrosh 2020 年 5 月 8 日
編集済み: Mahrosh 2020 年 5 月 8 日
Thanks steven for for your addition. For different interval can i define like this?
a = 0:0.01:0.9;
b = 0.9:0.001111111111111111:1;
x = [a,b(2:end)];
c=linspace(0,0.9,91);
d=linspace(0.9,1,91);
z = [c,d(2:end)];
If yes, then its giving me the same result as suggested by Rik.
Steven Lord
Steven Lord 2020 年 5 月 8 日
I probably have written the increment slightly differently for clarity:
b = 0.9:(1/900):1;
While there's still a magic number involved, I find they're often easier to understand and/or explain (depending on whether I'm reading or writing the code) when they're integer values (or the reciprocal of an integer value) than when they're some number written out to several decimal places.
Mahrosh
Mahrosh 2020 年 5 月 8 日
Thankyou steven for further explanation. I want to ask there is any effect on my calculation If I used number in decimal instead of intergers?
Rik
Rik 2020 年 5 月 9 日
There isn't any difference in the result, only in how you read the code as a human.

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 5 月 8 日

コメント済み:

Rik
2020 年 5 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by