Mathematica Count from MatrixForm in mathematica

  • Thread starter Thread starter 76Ahmad
  • Start date Start date
  • Tags Tags
    Count Mathematica
Click For Summary
To count the zero elements in a matrix in Mathematica, avoid using MatrixForm, as it is primarily for display purposes and does not support further processing. Instead, work with the matrix in its raw form. For example, you can use the Count function on a flattened version of the matrix to accurately count the zeros. The correct approach is to first create your matrix and then apply Count to Flatten[m], yielding the desired result. Using MatrixForm will not provide the correct count of zeros.
76Ahmad
Messages
46
Reaction score
0
Count from "MatrixForm" in mathematica!

Hi every one, I really need help on this task..

how do I Count only the zeros elements from a MatrixForm in MATHEMATICA

Suppose I have a Matrix 100X100 full with numbers and
I just need to know how many zero is there on this matrix

thanks
 
Physics news on Phys.org


First, all the various "Form"s are wrappers that turn an ordinary simple Mathematica value into something for display and aren't usually for further processing.

So, if you have your matrix before you put it into MatrixForm then you can do this

In[1]:= m=Table[RandomInteger[{5},{5}]

Out[1]= {{1,0,1,1,1},{0,1,1,0,0},{1,0,0,1,0},{0,0,0,0,0},{1,0,1,1,0}}

In[2]:= Count[Flatten[m],0]

Out[2]= 14

and that has successfully counted the zeros in the matrix.

But

In[4]:= Count[MatrixForm[m],0]

Out[4]= 0

In[5]:= Count[Flatten[MatrixForm[m]],0]

Out[5]= 0

So if you want to see a pretty desktop published version of your matrix... use MatrixForm, but if you want to get any work done on your matrix then do not use MatrixForm.
 


thanks its work 100%
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 18 ·
Replies
18
Views
5K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K