Message Boards Message Boards

0
|
2991 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Applying a function on a different reading frames

Posted 9 years ago

To give a better idea of what I'm asking, lets say I have the string

String={a,b,c,d,e,f,g,h,i}

Is there any way I could have a function add 1 to the first n terms (lets say n=4) so that I get Result 1:

{a+1,b+1,c+1,d+1,e,f,g,h,i}

and then also have the function apply again to the next n terms excluding the first, but shifted over one such that we have Result 2:

{a, b+1,c+1,d+1,e+1,f,g,h,i}

I'm looking for a way to design a program that can read through an entire string of letters, and consistently apply an "If" statement to read a particular frame. Currently I am capable of applying the If statement to a given frame of size n, but if the frame is located within a longer string then I have no way of reading through each frame one at a time.

One suggestion I have had is to structure the language here such that after I read through the first frame and have my first result, to have the first letter in the string deleted, and have the If function applied again to the new "first" n terms.

For example, Result 1:

{a+1,b+1,c+1,d+1,e,f,g,h,i} 

and then the second one would be Result 2:

{b+1,c+1,d+1,e+1,f,g,h,i} 

I would also need a way to display all of these results, like big matrix of matrices! That's something I'll get to later though.

Any thoughts, advice, feedback, or help otherwise would be greatly appreciated! Please leave any questions here if my question wasn't very clear or worded properly and I'll get back to you as soon as possible.

P.S. I am very new here (first post, woo!!) So please excuse my naivete and lack of technical jargon!

POSTED BY: Erik Gentzel
3 Replies

Thank you both for the feed back and advice!

POSTED BY: Erik Gentzel
Posted 9 years ago

Erik, you might also look into ListConvolve. In particular, look for how you can use your own custom functions in the convolution. For example:

ListConvolve[{y, z}, {a, b, c, d, e}, {2, 1}, {}, f, g]

outputs

{g[f[z, a], f[y, b]], g[f[z, b], f[y, c]], g[f[z, c], f[y, d]], g[f[z, d], f[y, e]]}

So, the function "f" would be like your "+1" function, and g would be whatever you want to do with that chunk (or just List if you just want to capture the chunk as a list). Here's a specific example:

In[19]:= ListConvolve[{1, 1, 1, 1}, Range[10], {4, 1}, {}, Plus, List]

Out[19]= {{2, 3, 4, 5}, {3, 4, 5, 6}, {4, 5, 6, 7}, {5, 6, 7, 8}, {6, 7, 8, 9}, {7, 8, 9, 10}, {8, 9, 10, 11}}
POSTED BY: Eric Rimbey

Hi Erik,

I think you need the function MapAt. You can do it like so:

args = {a, b, c, d, e, f, g, h};
MapAt[f, args, List /@ Range[3, 6]]
(* Out: {a,b,f[c],f[d],f[e],f[f],g,h} *)

This maps any function f onto those elements of the list args whose index lay within the "frame", i.e. within the range [3..6]. See the documentation for MapAt.

Just another remark: You should never begin the name of own variables with a capital letter! For this reason already the very first line of your example should result in an error message: String is a reserved word and has a build in meaning (apart from the fact that this is not a string but a list ...).

Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract