Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.3K Views
|
6 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Take the upper triangularize of a matrix

Hi,

I have a matrix like this mtr = {{a, b, c, d}, {x, y, z, c}, {1, 2, 3, 4}, {3, 6, 8, 9}};

I applied this operation

cc = UpperTriangularize[mtr]

so I have .

cc = {{a, b, c, d}, {0, y, z, c}, {0, 0, 3, 4}, {0, 0, 0, 9}}

**Now I would create a vector with only the values different to zero. How do I do? Hence I would obtain the follow resut: cc = {a,b,c,d,y,z,c,3,4,9}**

Thanks for your help.

Margherita

6 Replies

Hi,

this works:

Flatten[DeleteCases[#, 0, Infinity]] & /@ cc

Cheers,

Marco

POSTED BY: Marco Thiel

Start with the upper triangular matrix.

cc = {{a, b, c, d}, {0, y, z, c}, {0, 0, 3, 4}, {0, 0, 0, 9}};

If you want to remove the lower part but keep zeros, if any, that occur in the upper part, you can just extract the appropriate diagonals.

Flatten[Map[Diagonal[cc, #] &, Range[0, Length[cc] - 1]]]

(* Out[60]= {a, y, 3, 9, b, z, 4, c, c, d} *)

If you want to remove all zeros, there is the method shown by @Marco Thiel. An alternative is to convert to an explicit SparseArray and extract all but the default value, as below.

Most[ArrayRules[SparseArray[cc]][[All, 2]]]

(* Out[66]= {a, b, c, d, y, z, c, 3, 4, 9} *)
POSTED BY: Daniel Lichtblau

Hi,

this could work:

DeleteCases[Flatten[cc], 0]

Cheers,

Marco

POSTED BY: Marco Thiel

thank you a lot!

Thanks, a lot but ...Another help please,,, if I have cc = {{{a, b, c, d}, {0, y, z, c}}, {{0, 0, 3, 4}, {0, 0, 0, 9}}}; Which are the best commands to create this :

cc ={ {a, b, c, d, y, z, c}, {3, 4, 9} }

Delete th zero values and to maintain the same level.

Best regards.

Margherita

Thank you!

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