0

I am a beginner in Mathematica. How can I turn the following Matlab commands into Mathematica commands.

a=1;
b=2;
c=3;
d=4;
if ((a<4 || b>5) && ((c==3 || d<=6) && (c+d<10)))
    a=10;
    b=20;
    c=30;
    d=40;
elseif ((a<=2) && (c==3 || d<=6))
    a=100;
    b=200;
    c=300;
    d=400;
else
    a=1000;
    b=2000;
    c=3000;
    d=4000;
end
Math-Data
  • 103
  • 1
  • 1
  • 2

2 Answers2

4

Here is a direct translation of your if expressions using Mathematica's Which:

a = 1; b = 2; c = 3; d = 4;

Which[
  (a < 4 || b > 5) && ((c == 3 || d <= 6) && (c + d < 10)),
    a = 10; b = 20; c = 30; d = 40,
  (a <= 2) && (c == 3 || d <= 6),
    a = 100; b = 200; c = 300; d = 400,
  True,
    a = 1000; b = 2000; c = 3000; d = 4000
]

Note the use of compound expressions linked together using ;. See also Understand that semicolon is not a delimiter.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
1

The Mathematica commands If and And should do the trick:

If[query, "if true, run this code", "if false, run this code"]

Using two If functions should do the trick. The function And should be pretty self-explanatory.

As a beginner, putting a question mark (?) before any function will give you a rundown of it.

Sham Says
  • 341
  • 2
  • 11