[Date Index]
[Thread Index]
[Author Index]
Re: Two questions about modification of a matrix.
*To*: mathgroup at smc.vnet.net
*Subject*: [mg101068] Re: [mg101041] Two questions about modification of a matrix.
*From*: "Scot Martin" <smartin at seas.harvard.edu>
*Date*: Tue, 23 Jun 2009 07:05:23 -0400 (EDT)
*Organization*: Harvard University
*References*: <200906220822.EAA09140@smc.vnet.net>
*Reply-to*: <scot_martin at harvard.edu>
>> 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: [mg101068] [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)
Prev by Date:
**Re: Needs[], BeginPackage[], filename**
Next by Date:
**Re: Two questions about modification of a matrix.**
Previous by thread:
**Two questions about modification of a matrix.**
Next by thread:
**Re: Two questions about modification of a matrix.**
| |