Message Boards Message Boards

0
|
6202 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Extract part of a Table with the Wolfram Language?

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

There is someone who can help me?

5 Replies

Thanks for the answer Gustavo, I solved with this:

ascisse = Table[sort[[i]][[1]], {i, 1, npunti}];

ascisse2 = DeleteDuplicates[ascisse];

nscisse2 = Length[ascisse2];

vettoreord = Table[{}];

For[i = 1, i < nscisse2 + 1, i++,

riga = Cases[sort, {ascisse2[[i]], , }];

ordinateriga = Table[riga[[i]][[2]], {i, 1, Length[riga]}];

psi = Table[riga[[i]][[3]], {i, 1, Length[riga]}];

mediaord = Mean[ordinateriga];

vettoreord = Append[vettoreord, mediaord]];

The problem was that some abscissa were assosiated to more ordinates. Really thank you for yours advice.

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.

POSTED BY: Gustavo Delfino
Posted 9 years ago

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.

POSTED BY: Bill Simpson

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.

Posted 9 years ago

Perhaps you can adapt something like this

First create come concrete data to see how this works

In[1]:= sort = RandomInteger[{0, 5}, {6, 2}]

Out[1]= {{5, 3}, {4, 3}, {2, 2}, {5, 2}, {4, 3}, {2, 1}}

Now suppose you want to pick out the items that begin with a 4

In[2]:= table = Cases[sort, {4, __}]

Out[2]= {{4, 3}, {4, 3}}

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.

POSTED BY: Bill Simpson
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