Message Boards Message Boards

0
|
10786 Views
|
8 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Create a function/method and return a matrix as a result?

Posted 7 years ago

This is my day one using Mathematica. What I'm trying to do now is to translate a piece of my Matlab code into Mathematica. Here is an MWE, where I defined a module called coeffs:

coeffs[n0_] := Module[{n = n0},
   c = Table[0, {i, n + 1}, {j, n + 1}]
         Do[c[[l + 1, 2]] = 20 , {l, 0, n}]];

I'm trying to use this module as a function in usual sense, which takes a scalar number as input, create an empty matrix, and change the entries in the second column to something else. However, calling this function by coeffs[2] gives a result of {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}.

It seems no entries in the second column has been changed. What's the error in my code? Thanks!!

POSTED BY: K X
8 Replies
Posted 7 years ago
coeffs[n0_] := SparseArray[{i_, 2} -> 20, {n0 + 1, n0 + 1}]
POSTED BY: Michael Helmle
Posted 7 years ago

Thanks. A dumb question - what does the underscore _ after i mean? Why i_, not just i?

POSTED BY: K X

actually his example should(could) read:

coeffs[n0_] := SparseArray[{_, 2} -> 20, {n0 + 1, n0 + 1}]

_ is called a Blank, and can be read as 'something'... so what the rule says: at position something comma 2 put value 20. where something can be any value, i.e. it describes a column.

i_

this can be read as "something that I will call i" such that you can do things like:

SparseArray[{i_, 2} -> i^2, {5, 5}]

it will plug in 1,4,9,16,25.

Read also the introduction to Wolfram Language:

https://www.wolfram.com/language/elementary-introduction/32-patterns.html

Note that patterns (these things with _ and so on) is at chapter 32, so it is reasonably advanced...

POSTED BY: Sander Huisman
Posted 7 years ago

Thanks very much for the pointers. They're most helpful!

POSTED BY: K X

Inside a module you need to put ; between your expressions. To create a so-called CompoundExpression

ClearAll[coeffs]
coeffs[n0_] := Module[{n = n0, c},
   c = Table[0, {i, n + 1}, {j, n + 1}];
   Do[
    c[[l + 1, 2]] = 20
    ,
    {l, 0, n}
    ];
   c
   ];

Moreover, it is a good idea to make c a local variable, finally you need to return something. That can be done by just putting c at the end (without semicolon). Or explicitly do so using Return.

POSTED BY: Sander Huisman

Side note:

   c = Table[0, {i, n + 1}, {j, n + 1}];

can be replaced by:

   c = ConstantArray[0, {n + 1,n + 1}];

which is similar to the function ones/zeros in Matlab but can be any value, including symbols, strings or ...

You can set all the values at once:

c[[1 ;; n+1, 2]] = 20

or even simpler:

c[[All, 2]] = 20
POSTED BY: Sander Huisman
Posted 7 years ago

Thank you very much!! I see. In fact, the code I posted is just a minimal working example, every single entry in a column will be assigned a different value, not always 20. But thanks for tricks. They'll be very useful!

One more quick question - to define a "function" in usual sense, shall I use Module or Block? What's the difference?

Again, thanks a lot!

POSTED BY: K X

There are entire topics on it and the differences are subtle:

http://community.wolfram.com/groups/-/m/t/1017668

Let's just say that generally you will use Module (or With if you have constants rather than variables) . But sometimes Block can be handy (and slightly faster) and can be used in nifty ways. Since you are a beginner, don't worry too much for now... If you really want to know, i recommend you to read the aforementioned topic, it explains it in a lot of detail!

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract