Plot x and y with two function

1 回表示 (過去 30 日間)
Dani D
Dani D 2016 年 4 月 25 日
編集済み: Star Strider 2016 年 4 月 25 日
Hello, I want plot x and y , but i can't
x = rand(65000,1);
if x <= 0.5
y = sqrt(x);
elseif x > 0.5
y = x^1.25;
end
plot(x,y);

回答 (1 件)

Star Strider
Star Strider 2016 年 4 月 25 日
I am not certain what you are doing with ‘x’ being random, but this will work:
x = rand(65000,1);
y = (x <= 0.5).*(sqrt(x)) + (x > 0.5).*(x.^1.25);
figure(1)
plot(x, y)
grid
  2 件のコメント
Dani D
Dani D 2016 年 4 月 25 日
No , I have if else. If less or equal 0.5 we have sqrt(x) else if greater than 0.5 we have x^1.25
Star Strider
Star Strider 2016 年 4 月 25 日
編集済み: Star Strider 2016 年 4 月 25 日
That is what my code does. It uses logical index ‘masking’ instead of an if block.
The logical mask is equal to 1 where the logical condition is true (and is multiplied by the value of the function at that value of ‘x’) and 0 where the logical condition is false, so the value for that particular condition would be 0. The code for ‘y’ adds the two conditions together with respect to their function values over each condition to create a continuous vector.
EDIT This is a much less efficient way of doing it, but it produces the same result:
x = rand(65000,1);
for k1 = 1:length(x)
if x(k1) <= 0.5
y(k1) = sqrt(x(k1));
elseif x(k1) > 0.5
y(k1) = x(k1)^1.25;
end
end
plot(x,y);

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

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by