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

  • Mathematica
  • Thread starter hadron23
  • Start date
  • Tags
    Mathematica
In summary, the conversation discussed how to convert a matrix with dimensions {4,4,1} to a matrix with dimensions {4,4} in Mathematica. The solution involved using the Map and Flatten functions to remove the extra dimension and produce a new matrix with the desired dimensions. Additionally, the conversation mentioned that ranges can be specified in Part using the ;; syntax, similar to MATLAB.
  • #1
hadron23
28
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
  • #2
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}
 
  • #3
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.
 

1. How do I convert the list {4,4,1} to {4,4} in Mathematica?

To convert a list with three elements to a list with only two elements in Mathematica, you can use the command Most[{4,4,1}]. This will return the list {4,4}. Alternatively, you can also use the Drop function to remove the last element from the list, like Drop[{4,4,1},-1].

2. Can I convert a list with more than three elements to a list with two elements using the same method?

Yes, the Most function and Drop function can be used to convert lists with any number of elements to lists with two elements. Just make sure to specify the correct number of elements to be removed from the list.

3. Is there a way to convert a list to a different format in Mathematica?

Yes, there are several ways to convert lists to different formats in Mathematica. One way is to use the List function to convert a list to a string, like List["a", "b", "c"] will return the string "abc". Another way is to use the Array function to convert a list to an array, like Array["a", {3}] will return the array {a[1], a[2], a[3]}.

4. How can I add two lists together in Mathematica?

To add two lists together in Mathematica, you can use the Plus operator. For example, {1,2,3}+{4,5,6} will return {5,7,9}. Alternatively, you can also use the Join function to combine two lists, like Join[{1,2,3},{4,5,6}] will return {1,2,3,4,5,6}.

5. Is there a way to sort a list in Mathematica?

Yes, there are several ways to sort a list in Mathematica. One way is to use the Sort function, which will sort the elements in ascending order. For example, Sort[{3,2,1}] will return {1,2,3}. Another way is to use the Reverse function, which will reverse the order of the elements in a list. For example, Reverse[{1,2,3}] will return {3,2,1}.

Similar threads

Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
10K
  • Programming and Computer Science
Replies
1
Views
684
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
906
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
Back
Top