| Author |
Comment/Response |
yehuda ben-shimol
|
09/27/10 04:28am
In Response To 'Re: Re: MapAt with All operator' --------- I have found some more spare minutes so I'll elaborate...
using Span (;; notation) is available only as indexes of lists and matrices. It is a more "friendly" syntax for Range which always work
now, to use MapAt for a sub matrix, or modify the matrix in several places you need to define each such position. This eventually mimics (internally) the behavior I posted earlier, it is not better nor worse using Map rather than MapAt (and vice versa).
you also need not "compute" the size of a matrix, there is a Dimensions function for that matter
so, combining all of this, I have implemented a small function that will fit your needs
MapSpanAt[fun_, mat_, rows_, cols_] :=
Module[{dim = Dimensions[mat], r, c},
r = Switch[rows, _Integer, Range[rows], All,
Range[First[dim]], _Span, Range @@ (rows /. All -> First[dim])];
c = Switch[cols, _Integer, Range[cols], All,
Range[Last[dim]], _Span, Range @@ (cols /. All -> Last[dim])];
MapAt[fun, mat, Tuples[{r, c}]]
]
now, if you define
aaa = {{1, 2 + I, 3 + I, 4 + I}, {5, 6 + I, 7 + I, 8 + I}, {9, 10 + I,
11 + I, 12 + I}};
and call for example
MapSpanAt[Im,aaa,All,2;;All]
it returns the desired result
yehuda
URL: , |
|