Beginner's Mathematica Q: {4,4,1} to {4,4} ?

  • Context: Mathematica 
  • Thread starter Thread starter hadron23
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

This discussion addresses the conversion of a 3D matrix of dimensions {4,4,1} to a 2D matrix of dimensions {4,4} in Mathematica. The user initially struggles with this task, comparing it to a straightforward operation in MATLAB. The solution involves using the Map[Flatten, A] function to achieve the desired dimensions, or alternatively, accessing the first layer of the array with A[[All, All, 1]]. Both methods effectively reduce the matrix to the required dimensions.

PREREQUISITES
  • Basic understanding of Mathematica syntax and functions
  • Familiarity with matrix dimensions and manipulation
  • Knowledge of array indexing in Mathematica
  • Experience with MATLAB for comparative understanding
NEXT STEPS
  • Explore the Map function in Mathematica for advanced data manipulation
  • Learn about array indexing techniques in Mathematica
  • Investigate the Flatten function and its applications
  • Compare matrix operations between Mathematica and MATLAB for better cross-platform skills
USEFUL FOR

Mathematica users, data scientists, and anyone transitioning from MATLAB to Mathematica who needs to manipulate multi-dimensional arrays effectively.

hadron23
Messages
28
Reaction score
1
Hi,

I have just started using Mathematica mainly for its symbolic math abilities; however, I have run into a small problem that I don't know how to fix.

I have generated a matrix of dimensions:

Dimensions[A] = {4,4,1}

And I need to convert this to a matrix of dimensions:

Dimensions[A] = {4,4}

This is easy in Matlab (just A(:,:,1) = []) but I don't have a clue how to do it in Mathematica.

Thanks!
 
Physics news on Phys.org
In[1]:= A=Table[x,{4},{4},{1}]
Out[1]= {{{x},{x},{x},{x}},{{x},{x},{x},{x}},{{x},{x},{x},{x}},{{x},{x},{x},{x}}}

In[2]:= Dimensions[A]
Out[2]= {4,4,1}

In[3]:= newA=Map[Flatten,A]
Out[3]= {{x,x,x,x},{x,x,x,x},{x,x,x,x},{x,x,x,x}}

In[4]:= Dimensions[newA]
Out[4]= {4,4}
 
Here's another example which is closer to the MATLAB syntax:

Code:
In[1]:= A=Array[x,{4,4,1}]
Out[1]= {{{x[1,1,1]},{x[1,2,1]},{x[1,3,1]},{x[1,4,1]}},
         {{x[2,1,1]},{x[2,2,1]},{x[2,3,1]},{x[2,4,1]}},
         {{x[3,1,1]},{x[3,2,1]},{x[3,3,1]},{x[3,4,1]}},
         {{x[4,1,1]},{x[4,2,1]},{x[4,3,1]},{x[4,4,1]}}}

In[2]:= A[[All,All,1]]
Out[2]= {{x[1,1,1],x[1,2,1],x[1,3,1],x[1,4,1]},
         {x[2,1,1],x[2,2,1],x[2,3,1],x[2,4,1]},
         {x[3,1,1],x[3,2,1],x[3,3,1],x[3,4,1]},
         {x[4,1,1],x[4,2,1],x[4,3,1],x[4,4,1]}}

Ranges in Part ( [[ ]] ) can be specified similarly to in MATLAB but with : replaced by ;;.
So the same result as Out[2] can also be produced using A[[1;;, 1;;, 1]] or A[[1 ;;, ;; -1, 1]] etc.
Note that you can't have ;; by itself -- for that you have to use All.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
4K
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
11K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K