⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myif.m

📁 遗传规划的matlab工具箱
💻 M
字号:
function result=myif(cond,actiontrue,actionfalse)
%MYIF    Calculates the result of an IF-THEN-ELSE statement.
%   MYIF(CONDITION,ACTIONTRUE,ACTIONFALSE) evaluates the CONDITION
%   and returns the result of evaluating ACTIONTRUE if CONDITION is
%   true, or the result of evaluating ACTIONFALSE if CONDITION is
%   false. If condition evaluates to any number different from 0,
%   CONDITION is true.
%
%   Input arguments:
%      CONDITION - the expression to evaluate (expression)
%      ACTIONTRUE - the action if CONDITION is true (expression)
%      ACTIONFALSE - the action if CONDITION is false (expression)
%   Output arguments:
%      RESULT - the result of evaluating ACTIONTRUE or ACTIONFALSE (double)
%
%   Copyright (C) 2003-2004 Sara Silva (sara@dei.uc.pt)
%   This file is part of the GPLAB Toolbox

% first make sure all matrices are the same size:
m=max([length(cond) length(actiontrue) length(actionfalse)]);
if length(cond)<m
   cond=repmat(cond,m,1);
end
if length(actiontrue)<m
   actiontrue=repmat(actiontrue,m,1);
end
if length(actionfalse)<m
   actionfalse=repmat(actionfalse,m,1);
end

result=zeros(size(cond));

b=cond;

% for those who evaluate true, do action true:
i=find(b~=0);
result(i)=actiontrue(i);


% for those who evaluate false, do action false:
i=find(b==0);
result(i)=actionfalse(i);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -