I'm looking for Matlab equivalent of c# condition ? true-expression : false-expression
conditional assignment. The most I know of is a = 5>2
, which is true\false assignment,
but is there any one line conditional assignment for
if condition a=1;else a=2; end
?
Matlab conditional assignment
matlab
Related Solutions
The first function in an m-file (i.e. the main function), is invoked when that m-file is called. It is not required that the main function have the same name as the m-file, but for clarity it should. When the function and file name differ, the file name must be used to call the main function.
All subsequent functions in the m-file, called local functions (or "subfunctions" in the older terminology), can only be called by the main function and other local functions in that m-file. Functions in other m-files can not call them. Starting in R2016b, you can add local functions to scripts as well, although the scoping behavior is still the same (i.e. they can only be called from within the script).
In addition, you can also declare functions within other functions. These are called nested functions, and these can only be called from within the function they are nested. They can also have access to variables in functions in which they are nested, which makes them quite useful albeit slightly tricky to work with.
More food for thought...
There are some ways around the normal function scoping behavior outlined above, such as passing function handles as output arguments as mentioned in the answers from SCFrench and Jonas (which, starting in R2013b, is facilitated by the localfunctions
function). However, I wouldn't suggest making it a habit of resorting to such tricks, as there are likely much better options for organizing your functions and files.
For example, let's say you have a main function A
in an m-file A.m
, along with local functions D
, E
, and F
. Now let's say you have two other related functions B
and C
in m-files B.m
and C.m
, respectively, that you also want to be able to call D
, E
, and F
. Here are some options you have:
Put
D
,E
, andF
each in their own separate m-files, allowing any other function to call them. The downside is that the scope of these functions is large and isn't restricted to justA
,B
, andC
, but the upside is that this is quite simple.Create a
defineMyFunctions
m-file (like in Jonas' example) withD
,E
, andF
as local functions and a main function that simply returns function handles to them. This allows you to keepD
,E
, andF
in the same file, but it doesn't do anything regarding the scope of these functions since any function that can calldefineMyFunctions
can invoke them. You also then have to worry about passing the function handles around as arguments to make sure you have them where you need them.Copy
D
,E
andF
intoB.m
andC.m
as local functions. This limits the scope of their usage to justA
,B
, andC
, but makes updating and maintenance of your code a nightmare because you have three copies of the same code in different places.Use private functions! If you have
A
,B
, andC
in the same directory, you can create a subdirectory calledprivate
and placeD
,E
, andF
in there, each as a separate m-file. This limits their scope so they can only be called by functions in the directory immediately above (i.e.A
,B
, andC
) and keeps them together in the same place (but still different m-files):myDirectory/ A.m B.m C.m private/ D.m E.m F.m
All this goes somewhat outside the scope of your question, and is probably more detail than you need, but I thought it might be good to touch upon the more general concern of organizing all of your m-files. ;)
MATLAB is an unusual choice for a large-scale projects and is as much suited for such task as assembler, COBOL or SQL. If you still choose MATLAB then at least automatically test the code! All kind of tests - integration tests, unit tests, load tests! And of course use a version control system.
As said, MATLAB was not created with large projects in mind therefore the only best practice regarding project structure, modules, coupling is the common sense.
If you are taking over an existing large MATLAB project then I am sorry with you, refactoring will be nightmare. If you are going to start a new large project with MATLAB then you are crazy - there are much better alternatives to MATLAB that are not that bad regarding numeric performance. Large project implies that almost all code is business logic, not numerics, therefore why for God's sake MATLAB?
Large project implies well structured components, which implies OO, which is the weak point of MATLAB because it sacrifices heap performance for numeric performance to the degree of unusability.
My experience:
- I spent years in in a half-million LOC MATLAB project.
- I have seen painless transition of multiple large MATLAB projects to C#.
With MATLAB you still have to use large amounts of Java for decent looking GUI, C/C++ MEX for fast not numeric parts like imports, maybe SQL, etc. With Java (or better C#) with a free numeric library you have only one language which is perfectly suited for everything you need in a large project.
I am not saying that MATLAB is bad - it rules for rapid prototyping and numerics! And Simulink has no alternatives (but can be compiled and used from everywhere).
Best Solution
For numeric arrays, there is another solution --
becomes
Advantage:
works wonderfully in parallel for vectors or large arrays - each item in
A
gets assigned depending on the corresponding condition. The same line works for:X
andY
are equal in sizeWarning:
Doesn't work gracefully with
NaN
s. Beware! If an element ofX
isnan
, or an element ofY
is nan, then you'll get aNaN
inA
, irrespective of the condition.Really Useful corollary:
you can use
bsxfun
whereCOND
andX
/Y
have different sizes.works for example where
COND
andX
/Y
are vectors of different lengths.neat eh?