Re: Two questions about modification of a matrix.
- To: mathgroup at smc.vnet.net
- Subject: [mg101072] Re: Two questions about modification of a matrix.
- From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
- Date: Tue, 23 Jun 2009 07:06:16 -0400 (EDT)
- References: <h1nevg$8s9$1@smc.vnet.net>
There must be 100s of ways to do this. Here's my version:
n = 5;
m = RandomReal[{-5, 5}, {n, n}]
1: MapIndexed[If[Times @@ #2 > n, 0, #1] &, m, {2}]
2: Map[If[# < 0, 0, #] &, m, {2}]
A slightly more beginner-friendly formatted version of 1 would be:
MapIndexed[
Function[{entry, indices}, If[Apply[Times, indices] > n, 0,
entry] ], m, {2}]
Cheers -- Sjoerd
On Jun 22, 10:22 am, Mayasky <alix.zh... at gmail.com> wrote:
> 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 elemen=
ts
> that satisfy a certain condition)