1
$\begingroup$

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.

$\endgroup$
2
  • 3
    $\begingroup$ You could try using Flatten[m, {{2}, {1}}] to do a "generalized transpose" and pick off your desired lengths from there. $\endgroup$ – J. M.'s torpor Jan 26 at 14:11
  • $\begingroup$ this works finely, nice! $\endgroup$ – cheese.burger Jan 26 at 14:45
2
$\begingroup$
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
$\endgroup$
2
  • $\begingroup$ Elegant technics. Thanks! $\endgroup$ – cheese.burger Jan 26 at 14:48
  • $\begingroup$ @cheese.burger, my pleasure. thank you for the accept. $\endgroup$ – kglr Jan 26 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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