If elseif else end function
10 ビュー (過去 30 日間)
古いコメントを表示
hi, i am writing the code that follows
L=1:1:10;
choice = menu('Choose the case','case1','case2','case3','case4');
if choice==1
a = 1;
b=2;
c=3;
elseif choice==2
a = 6;
b=8;
c=9;
else
a = 2;
b=6;
c=8;
end
x=(a/b)^c.*L;
plot(L,x)
but now i want to add case 4 which will plot all three case in one graph? i can do it by calling a1,a2,a3... and so on then plot L1, L2, L3 for 3 case above in 1 graph
but actually my programme is so much bigger than this simple example. Do u have any other suggestion?
0 件のコメント
回答 (2 件)
Matt J
2014 年 8 月 23 日
編集済み: Matt J
2014 年 8 月 23 日
Do more vectorized processing. The following is easily extendable to more data
a=[1 6 2];
b=[2 8 6];
c=[3,9,8];
coeff=(a./b).^c;
Then you can simply do
for choice=1:length(coeff)
plot(L,coeff(choice).*L); hold on
end
hold off
Image Analyst
2014 年 8 月 23 日
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
L=1:1:10;
a=[1 6 2];
b=[2 8 6];
c=[3,9,8];
choice = menu('Choose the case','case1','case2','case3','case4')
if choice<= 3
indexes = choice
else
indexes = 1:3
end
x= (a(indexes) ./ b(indexes)) .^ c(indexes) .* L(indexes);
plot(L(indexes), x, 'b*-', 'LineWidth', 2, 'MarkerSize', 15);
grid on;

0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!