Hi, I am not an expert with Mathematica, i’m just approaching to this software.
I should extract elements from a Table called ‘sort’, if the first term of the element is equal to a certain value.
How can I do this?
I have also developed a code, but I don’t know how can I implement in Mathematica:
table=Table[{}]
cont=0
cont2=0
For i=1 to npoint
If sort[[i]][[1]]=sort[[i+1]][[1]] Then
cont=cont+1
table=Table[{pippo,sort[[i+1]][[1]]}]
Elseif cont>0
cont2=cont2+1
table(cont2)=table
Print table(cont2)
cont=0
End if
Next i
If this isn’t enough and you can provide a simple clear description of what you need to accomplish then perhaps someone can take a moment to show you how to translate it into Mathematica.
Be careful with this if your data is going to include numbers with decimal points. It might work, but it might require other approaches to make sure that it correctly matches numbers which might display the same on the screen, but have very very tiny differences in the values that may not be visible.
Thanks for your answer.
This procedure works, but my problem is to automate the calculation in a loop.
In fact I need more or less 1300 extraction for all the different values ??that assumes the first component of each element of the table.
These values also include numbers with long decimal points.
It is difficult to understand exactly how and what to automate.
Perhaps this will help.
Create a concrete example
f = RandomReal[{0, 5}, 6];
unsort = Table[i = RandomInteger[6];
y = RandomReal[{0, 10}]; {f[[i]], y}, {1300}];
sort = Sort[unsort]
Now group together all lists which have the same first element
splitsort = Split[sort, First[#1] == First[#2] &]
Now examine the first set of items which share the first element
splitsort[[1]]
Perhaps this will be useful
For[n = 1, n <= Length[splitsort], n++,
Print["The ", n, "'th group with the same first element is: ", splitsort[[n]]]
]
If this is still not sufficient then can you provide the simplest clearest description of what you need as a result? Perhaps with that it will be possible to get closer to what you want to accomplish.
Francesco, there is an important difference between what you ask and the code you provide. If you just want to select the rows that meed a criteria, using Cases, Select or even Query is the way to go. But you code suggests that to test a line, you need information about the next line. In this case you need to look into using Partition first.
Avoid using the traditional For and While as they almost always lead to solutions more complicated than needed.