Message Boards Message Boards

0
|
2445 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Delete numbers below the diagonal in a table?

Posted 1 year ago

Hello, I have a big table from which I need to delete numbers below the diagonal of the table:

a=TableForm@{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 
   16}}
b = TableForm@{{1, 2, 3, 4}, {, 6, 7, 8}, {, , 11, 12}, {, , , 16}}

Such as a to b above. The table is big, so this is just a case. How to do it smartly? It is better to replace Null with empty. enter image description here

POSTED BY: Zhenyu Zeng
6 Replies
Posted 1 year ago

Another way is to use ReplacePart :

a = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
ReplacePart[a, {i_, j_} /; i > j -> Null]
POSTED BY: Hans Milton
Posted 1 year ago

Thanks! It is a simplest and best way to do so!

POSTED BY: Zhenyu Zeng

Perhaps have a look at the function UpperTriangularize.

POSTED BY: Sander Huisman
Posted 1 year ago

Hello, How do you know so many functions? And how to replace 0 with empty after using UpperTriangularize?

POSTED BY: Zhenyu Zeng

You would need to do some tricks and make your own function:

ClearAll[UpperTriangularizeNull]
UpperTriangularizeNull[m_?MatrixQ] := Module[{mask},
  mask = ConstantArray[Null, Dimensions[m]];
  mask = LowerTriangularize[mask, -1];
  mask + UpperTriangularize[m]
  ]
a = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
UpperTriangularizeNull[a]
POSTED BY: Sander Huisman
Posted 1 year ago

Thanks a lot. You know so much!

POSTED BY: Zhenyu Zeng
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract