# Specifying the position of the bottom element in a matrix when the last few elements are missing

Suppose I have a matrix (nested list) with some missing elements in the bottom region:

m={{1,5},{2,6},{3},{4}};
m//TableForm

1   5
2   6
3
4


As you see, there are two missing elements at the position of m[[3,2]] and m[[4,2]].

I want to specify the position of the bottom element in the 2nd column, i.e., I want to specify the position of the element just before the position of the missing data. In this example, at the 2nd column, the bottom element is 6 and lies at m[[2,2]]. Thus, the answer should be m[[2,2]].

Suppose that I do not know both the bottom value (6, in this case) and how many rows are missing in the 2nd column (because my real data has very long column).

To achieve this, I tried to calculate the length of the 2nd column:

Length@m[[All,2]]


But, this does not work, because part 2 of the 3rd and 4th rows do not exist. Any ideas would be appreciated.

• You could try using Flatten[m, {{2}, {1}}] to do a "generalized transpose" and pick off your desired lengths from there. Jan 26, 2021 at 14:11
• this works finely, nice! Jan 26, 2021 at 14:45

rownum = LengthWhile[m, Length@# == 2 &]

2

m[[rownum, 2]]

6


If you need the last element in the second column you can also use:

TakeWhile[m, Length@# == 2 &][[-1, 2]]

6

• Elegant technics. Thanks! Jan 26, 2021 at 14:48
• @cheese.burger, my pleasure. thank you for the accept.
– kglr
Jan 26, 2021 at 15:13