Access Matrix Element in Mathematica

In summary: If you want to access an element of a matrix in Mathematica, you first need to define the matrix and then use the Extract function. To access an element of a matrix in Matlab, you first need to define the matrix and then use the Get function.
  • #1
Mindscrape
1,861
1
[SOLVED] Access Matrix Element in Mathematica

Alright, so in Mathematica you define a matrix by saying A = {{a11,12,...},{21,a22,...}} for whatever matrix you are making. How do you access a particular element of that matrix?

Argh, Matlab is so much better.
 
Physics news on Phys.org
  • #2
Yep, Mathematica is the crappiest language I've ever come across, and I've worked with more than a few. :)

Use Extract, e.g.:

Extract[Extract[A,1],2] = a12

If possible, use IDL, Matlab, R, Octave, SciLab, Python with NumPy and SciPy or anything else. I'd rather use an abacus than use Mathematica :). Good luck with your work.

--------
Assaf
http://www.physicallyincorrect.com/"
 
Last edited by a moderator:
  • #3
If I recall correctly, L[[x]] extracts the x-th element of a list.

If you really don't want to type A[[x]][[y]], you can always define a function:

A[x_, y_] := A[[x]][[y]]

and then you should be able to use A[x, y] to index your matrix. (I think you can use the same letter A for both these purposes. If it fails or you find it confusing, then just rename the function to something else.

You could even do a generic

Get[A_, x_, y_] := A[[x]][[y]]

that takes the matrix as an argument. Then you'd call

Get[A, x, y]



Matlab was designed for the sole purpose of manipulating matrices -- therefore it should be no surprise that it's more convenient to use Matlab instead of Mathematica for manipulating matrices. :wink:

Mathematica isn't a bad language: the catch is that it's a functional language, which takes a bit of adjustment if you've never programmed in a functional language before.
 
Last edited:
  • #4
I guess it's a matter of taste. But I have worked with LISP & Haskell. Can't say they've changed my opinion about Mathematica :smile:

--------
Assaf
http://www.physicallyincorrect.com/"
 
Last edited by a moderator:
  • #5
Got it. Thanks guys, I would have never sorted through mathematica help well enough to figure this out.
 
  • #6


It really isn't that difficult - you can use Part[A, i, j]=aij
 

1. How do I access a specific element in a matrix in Mathematica?

To access a specific element in a matrix in Mathematica, you can use the double bracket notation. For example, if you have a matrix called "A" and want to access the element in the second row and third column, you would use A[[2,3]].

2. Can I access multiple elements in a matrix at once?

Yes, you can access multiple elements in a matrix at once by using a list of indices in the double bracket notation. For example, A[[{2,3}, {4,5}]] would access the elements in the second and third rows and fourth and fifth columns.

3. How can I access all elements in a specific row or column of a matrix?

To access all elements in a specific row or column of a matrix, you can use the colon notation. For example, A[[2, All]] would access all elements in the second row, while A[[All, 3]] would access all elements in the third column.

4. Is it possible to access a range of elements in a matrix?

Yes, you can access a range of elements in a matrix by using the colon notation with a range of indices. For example, A[[2, 2;;5]] would access elements in the second row from the second to fifth column.

5. Can I access elements in a matrix using logical conditions?

Yes, you can access elements in a matrix using logical conditions with the Select function. For example, Select[A, #>5&] would select all elements in matrix A that are greater than 5.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
21
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top