フィルターのクリア

If elseif else end function

39 ビュー (過去 30 日間)
son
son 2014 年 8 月 23 日
回答済み: Image Analyst 2014 年 8 月 23 日
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?

回答 (2 件)

Matt J
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
  2 件のコメント
Matt J
Matt J 2014 年 8 月 23 日
編集済み: Matt J 2014 年 8 月 23 日
Or,
N=length(coeff);
args(2,1:N)=num2cell(L.'*coeff,1);
args(1,:)={L};
plot(args{:});
son
son 2014 年 8 月 23 日
i mean the first 3 case above will produce 1 plot . and then the final case will produce 3 line in 1 graph.

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


Image Analyst
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;

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by