-1
  • How to convert the following Mathematica code into Matlab?

  • What is the corresponding Matlab code?

Clear[defvar]
defvar[{Nh_Symbol, Nhf_Symbol, gini_Symbol}, {dim_Integer, steps_Integer}] :=
  Module[{cN, cNf, ginix},
   cN = Array[Nh, dim];
   cNf = Array[Nhf, dim];
   ginix = Array[gini, steps];
   {cN, cNf, ginix}
  ];

Here is an example of using the code above:

Clear[Nh, Nhf, gini]
defvar[{Nh, Nhf, gini}, {10, 1}]

Original post

I am new to the forum and I am also a newbie to programming. I want to convert a Mathematica code to Matlab, but I don't understand how to translate these lines. This code is Mathematica 5.0.

defvar := Module[{a},
Clear[ cN, cNf, ginix ];
Clear[ Nh, Nhf, gini ];
cN = Array[Nh, dim]; cNf = Array[Nhf, dim];
ginix = Array[gix, steps];]

where dim = 1000; steps = 1.

I thank you very much!

Anton Antonov
  • 37,787
  • 3
  • 100
  • 178
  • The code shown would be bad even if V5.0 were current rather than ancient. I don't see how anyone could be expected to translate such bad code into good current Matlab without having a description of the problem domain from which the code is derived. – m_goldberg Aug 20 '21 at 00:59

1 Answers1

4

Let's look at your code to understand the problems and identify what it does and doesn't do.

This may enable you to develop the Matlab code you want. I don't think you'll get much help from this forum in doing a translation.

Formatting the so you have each sub-statement on a single line will help you understand it a bit better:

defvar := Module[{a},
  Clear[cN, cNf, ginix];
  Clear[Nh, Nhf, gini];
  cN = Array[Nh, dim];
  cNf = Array[Nhf, dim];
  ginix = Array[gix, steps];
  ]

defvar := ... defines a bespoke function and the left side of an expression.

In the code defvar has no parameters passed to it, which it could if you have code something like the following, which enables one to pass 3 parameters to the function:

defvar[x_, y_, z_]:= something...

Everything within the right side of the expression resides in the native Mathematica function Module, which ring fences the variable shown with the curly brackets {a}.

First problem, the function defvar doesn't use a for anything. It has a local variable, but it doesn't use it.

Next, you have two statements which use Clear:

  Clear[cN, cNf, ginix];
  Clear[Nh, Nhf, gini];

These statements clear the value of the six listed variables: cN, cNf, ginix,Nh, Nhf, and gini. First thought, these look like global variables in a Notebook, but they could also reside in i.e., a higher level function or Module.

The code then assigns values to the "global varialbescN, CNf, and ginix.

Since the code, preceding these value assignments, clears Nh, Nhf, and ginix the assignments for these variables appear to only assign symbolic values.

The code posted does not have any internally received or generated values for dim or steps or gix.

fix would set symbolically, but unless the defvar function can access integers for dim and steps the array generation won't work.

dim and steps may exist as global variable in some larger code.

Finally, while the defvar function may do something in the right larger environment, it does not return anything. It has no output.

As example...

defvar := Module[{a},
  Clear[cN, cNf, ginix];
  Clear[Nh, Nhf, gini];
  cN = Array[Nh, dim];
  cNf = Array[Nhf, dim];
  ginix = Array[gix, steps]
  ]

notice that I removed the last semicolon in the code. This would enable the function to return the defined array, ginix.

Another possibility...

Something like the following would enable it to output all of the defined arrays:

    defvar := Module[{a},
      Clear[cN, cNf, ginix];
      Clear[Nh, Nhf, gini];
  {
    cN = Array[Nh, dim],
    cNf = Array[Nhf, dim],
    ginix = Array[gix, steps]
  }
  ]

but it's hard to speculate about what you want to do without more information.

Jagra
  • 14,343
  • 1
  • 39
  • 81
  • Check this package for transforming syntax to matlab. https://library.wolfram.com/infocenter/MathSource/577/ – qahtah Aug 20 '21 at 12:59
  • @qahtah -- Great link, but maybe better to add it as a comment on the OP's original post rather than an answer. That said, post it as an answer and I'll upvote it. – Jagra Aug 20 '21 at 22:31
  • Thank you @Jagra for your amability. The code of question is a extract of code in this next link (bottom of page): https://faculty.wcas.northwestern.edu/~mdo738/research.html, of paper "Inequality and Growth: Why Differential Fertility Matters". Thank you! – Juan Angel Aug 21 '21 at 18:51
  • Thank you @qahtah! – Juan Angel Aug 21 '21 at 18:52