Skipping numbers in lists in Mathematica

In summary, the conversation is about a person who is new to using Mathematica and is trying to manipulate a large file of numbers for research. They have already figured out how to generate a histogram, but now they want to skip over certain numbers and save them under the same name for a new histogram. They have tried using the takedata option, but it did not work. Another person suggests using a For [ ] - loop and writing the operation in the body, or using a Do loop. The first person then presents their code using a Do loop, but it needs to be evaluated 256 times with different values for initobs. They tried putting the whole expression in a Do loop, but it did not work. They are seeking
  • #1
laminatedevildoll
211
0
Hi,

I am new to mathematica, and part of the program I have to write includes reading a file and manipulating the data for research. This file has around 22,000 numbers listed in it. I've already figured out how to look at those numbers and generate a histogram. However, what if I want to skip over numbers, meaning that now I want to read every 256th number until the end of my data set and save it each time under the same name, i.e data1, so I can generate a new histogram with these numbers. I've already tried the takedata option, without much luck.

I'd appreciate any help.
 
Physics news on Phys.org
  • #2
One simple way to do it would be to write a For [ ] - loop over your data with the appropriate incrementation and writing the operation (the save) in the body of the for-statement.
 
  • #3
Okay, I figured out how to skip numbers by using a Do loop.

Here's my code so far...

data = BinaryReadList[file,"Integer16"]
lngth = Length[data]
stepsize = 255
numiter = IntegerPart[lngth/stepsize]
initobs=1
data1={}
Do[data1=Append[data1,data[[i]]], {i,initobs,numiter*stepsize,stepsize}]
Readlist[data1]

However, I need to evaluate this 256 times where initobs = 1,2...256. I tried to put this whole expression in a do loop, so that it could output 256 different list for different values of initobs, but it doesn't work. What am I doing wrong?
 

1. How do I skip numbers in a list in Mathematica?

To skip numbers in a list in Mathematica, you can use the Range function and specify the step size. For example, if you want to skip every other number in a list from 1 to 10, you can use Range[1, 10, 2]. This will give you the list {1, 3, 5, 7, 9}.

2. Can I skip numbers in a list based on a specific condition?

Yes, you can use the Select function in Mathematica to skip numbers in a list based on a condition. For example, if you have a list of numbers from 1 to 10 and you only want to include numbers that are greater than 5, you can use Select[list, #>5&]. This will give you the list {6, 7, 8, 9, 10}.

3. Is it possible to skip numbers in a list while maintaining the order?

Yes, you can use the DeleteCases function to skip numbers in a list while maintaining the order. For example, if you have a list of numbers from 1 to 10 and you want to skip all even numbers, you can use DeleteCases[list, _?EvenQ]. This will give you the list {1, 3, 5, 7, 9}.

4. How can I skip numbers in a list and replace them with a different value?

You can use the ReplacePart function in Mathematica to skip numbers in a list and replace them with a different value. For example, if you have a list of numbers from 1 to 10 and you want to skip all multiples of 3 and replace them with the string "skipped", you can use ReplacePart[list, Position[list, _?Divisible[#, 3]&] -> "skipped"]. This will give you the list {1, 2, "skipped", 4, 5, "skipped", 7, 8, "skipped", 10}.

5. Can I skip numbers in a list and perform operations on the remaining numbers?

Yes, you can use the Map function in Mathematica to perform operations on a list after skipping certain numbers. For example, if you have a list of numbers from 1 to 10 and you want to square each number after skipping all multiples of 2, you can use Map[Power[#, 2]&, Select[list, Not[#%2==0]&]]. This will give you the list {1, 9, 25, 49, 81}.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
268
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • Programming and Computer Science
Replies
11
Views
992
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top