toggle and elseif in matlab

16 ビュー (過去 30 日間)
Shivaani S
Shivaani S 2020 年 5 月 4 日
コメント済み: Walter Roberson 2024 年 8 月 16 日
Try adding a variable doDisplay to toggle if the densities are displayed. Add this condition with an elseif block.
i do not understand what we are supposed to do in the above question(bold). this question is a part of futher practice in mathlab oramp. decision branching topic futher practice section in matworks course for basic matlab. how do i solve tha bove question
  1 件のコメント
Misbah Rafi Farooqui
Misbah Rafi Farooqui 2020 年 7 月 27 日
write "doPlot>=1"

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

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 5 月 4 日
Shivaani - I think that all the code is asking you to do is to add another variable called doDisplay and an elseif that would check if this variable is true. If so, then you would plot the densities. So there would be two ways to plot the data - either through the doPlot or with the doDisplay
if doPlot
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
elseif doDisplay
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
else
disp("The density of " + element ...
+ " is " + density)
end
You would need to declare and initialize the doDisplay variable too.
  4 件のコメント
Geoff Hayes
Geoff Hayes 2020 年 5 月 5 日
I'm not sure then...I added the above code and was able to proceed/pass. Are you sure that you have defined doDisplay?
Elisa R
Elisa R 2020 年 8 月 4 日
This is what I did. I ran it numerous times. both doPlot and doDisplay have to be 0 in this scenario in order for the text to display instead of the plot.
doPlot = randi([0 1])
load datafile
density = data(:,1);
doDisplay = randi([0 1])
if doPlot == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
elseif doDisplay == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
else
disp("The density of " + element ...
+ " is " + density)
end

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

その他の回答 (7 件)

Varot Pakavatsoontorn
Varot Pakavatsoontorn 2021 年 5 月 1 日
Hi, I think the instructor might mean the following:
doDisplay = 1-doPlot
if doPlot == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
elseif doDisplay == 1
disp("The density of " + element ...
+ " is " + density)
end
  2 件のコメント
Simone Silva
Simone Silva 2022 年 3 月 7 日
Hi.
The instructor asks what hapenned when doPlot and doDisplay are both equal one or they both equal zero.
If we define doDisplay=1-doPlot, the cases above are not possible.
Rik
Rik 2022 年 4 月 7 日
Comment posted as flag by ANAHITA SOLTANTOUYEH:
This is imo the best interpretation of the task at hand and a great solution.

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


Saif Alameri
Saif Alameri 2022 年 8 月 26 日
My interpretation to this practice is to create another variable (doDisplay) with random number (0 or 1) just like the (doPlot), then use it as a second condtion for (elseif):
doPlot = randi([0 1])
doDisplay=randi([0 1])
load datafile
density = data(:,1);
if doPlot == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
elseif doDisplay == 1
disp("The density of " + element + " is " + density)
end
When:
doPlot = 0 and doDisplay = 0, then nothing displaied
doPlot = 1 and doDisplay = 0, then plot displaied
doPlot = 0 and doDisplay = 1, then density displaied
doPlot = 1 and doDisplay = 1, then only plot displaied
The point of this practice is to explain that second condtion will executing only when it met it's condtion and first condtion is not.
  1 件のコメント
DGM
DGM 2024 年 8 月 16 日
I think this is the correct and most direct interpretation of the instructions. I can see why a person would be confused though, as it doesn't really make sense to use if/elseif for this. To me, it would be more sensible to do
if doPlot
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
end
if doDisplay
disp("The density of " + element + " is " + density)
end
Otherwise, there's not really any point in using multiple flags to control the behavior. As much as I think it's better, this isn't what the instructions describe.

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


Kakasaheb Nikam
Kakasaheb Nikam 2020 年 5 月 14 日
a Simple trick that i follow is,
at beginning I also not understand,
so we read it thrice by breaking sentance.
apply simple common logic.
we understand it.

Joanne Middour
Joanne Middour 2021 年 2 月 13 日
This is the way I did it because I was under the impression that they are asking us to do recursion here, but I may be wrong.
doDisplay = randi([0 1])
if doPlot == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
elseif doDisplay == 1
doPlot = 1
else
disp("The density of " + element ...
+ " is " + density)
end
  1 件のコメント
ABINESH R
ABINESH R 2022 年 7 月 2 日
they are also asking what happens when they both equal zero, or when only doDisplay equals one?What's the ans for this

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


sai
sai 2021 年 4 月 4 日
if doPlot == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
elseif doDisplay
disp("The density of " + element ...
+ " is " + density)
end

kunku S M
kunku S M 2022 年 2 月 3 日
try this, i think it's the answer
doDisplay=randi([0 1])
if doPlot == 0
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
elseif doDisplay == 1
disp("The density of " + element ...
+ " is " + density)
end

Muhammad
Muhammad 2024 年 8 月 7 日
編集済み: Muhammad 2024 年 8 月 8 日
I have tried: Here we can test what happens when both are equal to 1 or zero or only doDisplay is equal to 1. Nothing happens when both are equal to zero. When both are equal to 1, graph is printed. When only doDisplay is 1, it displays the densities. But still seems to be tricky quetion.
doPlot = randi([0 1])
doDisplay = randi([0 1])
density =
if doPlot == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
elseif doDisplay ~= doPlot
disp("The dinsity of " + element ...
+ " is " + density)
else
doPlot
doDisplay
end
doPlot = 1
doDisplay = 0
Unrecognized function or variable 'density'.
  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 8 月 16 日
density =
That line is not valid MATLAB syntax.

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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by