4
$\begingroup$

I know this question. If there are an equal number of columns on each row, then Number of rows: $Length[[MatrixName]]$ and number of columns: $Length[MatrixName[[1]]]$. However, I have a big dataset whose each row does not have an equal number of columns. How can I find the number of rows of a particular column or the number of columns for a particular row?

$\endgroup$

1 Answer 1

7
$\begingroup$
SeedRandom[1234];

(mat = Take[#, RandomInteger[{3, 5}]] & /@ 
    Array[a, {10, 10}]) // Column

enter image description here

nbrCol[mat_, row_Integer?Positive] := Length@mat[[row]]

nbrCol[mat, #] & /@ Range[10]

(* {5, 3, 3, 4, 5, 5, 4, 4, 5, 5} *)

nbrRows[mat_, col_Integer?Positive] := 
 Count[mat, _?(Length[#] >= col &)]

nbrRows[mat, #] & /@ Range[10]

(* {10, 10, 10, 8, 5, 0, 0, 0, 0, 0} *)
$\endgroup$
1
  • $\begingroup$ Thanks for your reply. $\endgroup$
    – PoreyS
    Commented Feb 10, 2021 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.