Re: Beginner: List Indexing and Assignment
- To: mathgroup at smc.vnet.net
- Subject: [mg94515] Re: Beginner: List Indexing and Assignment
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Mon, 15 Dec 2008 07:46:11 -0500 (EST)
- References: <gi2ut4$a5h$1@smc.vnet.net>
Hi, > I am moving from another system to Mathematica and have a few simple > questions about indexing and altering lists. I've been able to find > Mathematica equivalents to some of the other system's idioms, but > as a Mathematica neophyte they're not very elegant. I'd be very > grateful if someone could tell me the Mathematica equivalents---or > point me to a suitable Rosetta stone (Google didn't easily turn one up). The heart and strongest part of the other system is handling numeric matrices and manipulating these. If you compare Mathematica with the other system in exactly that field, you will not always, but often be disappointed. At the heart of Mathematica is pattern matching and functional programming, and if you want elegant (or brief, or efficient) solutions, you will need to take advantage of the strong parts of Mathematica. > In the other system, I would create a 2x3 matrix using > > a = [1 2 3; 4 5 6] > > resulting in > > [1 2 3] > [4 5 6] > > and then assign any element of the matrix whose value is greater than 2 the value -1 using > > a(a>2) = -1 > > resulting in > > [ 1 2 -1] > [-1 -1 -1] > > I can do this in Mathematica by: > > a = ReplacePart[a, Position[a, x_ /; x > 2] -> -1] > > but is there a more elegant method? You have asked for list indexing, but your example does only address a list indexing problem when thinking in terms of the other system. What I see is that you want to change all entries of a matrix that are larger than 2 to be -1. This is what probably comes to a Mathematica users mind first: mat = {{1,2,Pi},{4,5,6}} mat = mat /. n_ /; n>2 -> -1 What it does is this: replace ( /. is short for ReplaceAll) everything within mat with minus one, provided that (/; is short for Condition) it is larger than 2 and reassign the result to mat. Looking at your original solution, you have not been far away, it was just your prior experience that did make you think that you would need to somehow create indices, but that is not necessary when using patterns. Of course the above way of doing things has disadvantages: pattern matching is very general and powerful, but for simple tasks like this usually has too much overhead and thus is not very fast. The above code will also make a copy of a, so for large matrices the above will not be a good solution. There are many possibilities to do this more efficiently also in Mathematica, but most of these would be more elaborate. Here is a try that is short and should be reasonable fast even for larger matrices (as long as a copy fits in memory): a = Clip[a, {0, 2}, {0, -1}] Oh, and it could be changed to handle cases with negative entries also: a = Clip[a, {-Infinity, 2}, {-Infinity, -1}] That approach will still make a copy of a and is not as short as in the other system, but probably you see that with a slightly different kind of thinking you can get your problems solved with Mathematica without too much effort. All in all for this very special task the other system wins in many respects, but of course there are many other tasks, where the outcome will be (very) different. It really depends on what you plan to do and of course how you think about your problems and how you formulate possible solutions. If operations on numerical matrices is all you are doing, all you will be doing in the future and all you can think of (that should by no means be interpreted that I suggest you couldn't :-)), a move to Mathematica will probably not pay off... hth, albert
- Follow-Ups:
- Re: Re: Beginner: List Indexing and Assignment
- From: C Rose <cjr.list.alias.1@me.com>
- Re: Re: Beginner: List Indexing and Assignment