TransWikia.com

Rows within a norm

Mathematica Asked on May 31, 2021

Is there a way to take a matrix and select the rows (or columns) with norm lesser than a certain amount max. Thanks

One Answer

SeedRandom[1]
mat = RandomInteger[100, {5, 5}];

TeXForm @ MatrixForm @ mat

$left( begin{array}{ccccc} 80 & 14 & 0 & 67 & 3 65 & 100 & 23 & 97 & 68 74 & 15 & 24 & 4 & 100 90 & 83 & 70 & 1 & 30 48 & 25 & 44 & 73 & 69 end{array} right)$

N[Norm /@ mat]
 {105.328, 169.667, 127.644, 144.187, 122.291}
max = 130;

You can use Select, Pick, Cases or DeleteCases as follows:

Select[Norm[#] <= max &] @ mat
Pick[mat, Norm[#] <= max & /@ mat ]
Pick[mat, UnitStep[ max - Norm /@ mat ], 1]
Cases[_List?(Norm[#] <= max &)] @ mat
DeleteCases[_List?(Norm[#] > max &)] @ mat

all give

{{80, 14, 0, 67, 3}, {74, 15, 24, 4, 100}, {48, 25, 44, 73, 69}}

To select columns with norm $le max$, use Transpose[mat]:

Select[Norm[#] <= max &]@Transpose[mat]
{{0, 23, 24, 70, 44}}

Correct answer by kglr on May 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP