Re: Two questions about modification of a matrix.
- To: mathgroup at smc.vnet.net
- Subject: [mg101079] Re: [mg101041] Two questions about modification of a matrix.
- From: Chenguang Zhang <alix.zhang at gmail.com>
- Date: Tue, 23 Jun 2009 07:07:33 -0400 (EDT)
- References: <200906220822.EAA09140@smc.vnet.net>
Thanks, you excellent people! I really did not expect there to be so great ways for that. On Mon, Jun 22, 2009 at 11:18 PM, Scot Martin <smartin at seas.harvard.edu>wrote: > >> I know a "for loop" can be used, I just wonder if there is any more > concise and beautiful ways. > > You're right about Mathematica. Stop thinking about loops. Think about Map > and Pattern. Here are some example solutions to the problems you posed. > > 1. n = 3; MapIndexed[(If[Times @@ #2 > n, 0, #1]) &, mat, {2}] > > 2. mat /. {_?Negative->0} (*This "?" is known as a PatternTest[]*) > > > > -----Original Message----- > From: Mayasky [mailto:alix.zhang at gmail.com] > Sent: Monday, June 22, 2009 04:22 > To: mathgroup at smc.vnet.net > Subject: [mg101041] Two questions about modification of a matrix. > > Suppose there is a N*N matrix. > > 1. How can I change all elements that satisfy i*j>N to 0 ? (Here i, j > is position of this element, as in mat[[i,j]]) > > 2. How can I change all negative elements in that matrix to 0 ? > > I know a "for loop" can be used, I just wonder if there is any more > concise and beautiful ways. (I doubt Mathematica doesn't have a 'find' > function as in another system which conveniently returns indice of elements > that satisfy a certain condition) > > >
- References:
- Two questions about modification of a matrix.
- From: Mayasky <alix.zhang@gmail.com>
- Two questions about modification of a matrix.