1
$\begingroup$

I am quite new in Mathematica and I really need help with something ... I have an excel file with a total of 5332 rows and 6 columns. Column 1 is my focus of interest. The first 2666 data of column 1 are descending from 190 to 30 and the rest are ascending from 30 to 190. I would like to import the data (all six columns) of rows for which the column 1 data are ascending from 60 to 160. I have tried using "Select" without any success. I'm also not sure at all of using both "Import" and "Select" in combination. Any help will be greatly appreciated!!

Best Wishes Naz

$\endgroup$
2
$\begingroup$

Since you want ascending, and that depends on comparing pairs of items, we can group together each adjacent pair of items, test each pair to see if it qualifies and then take the first item from each pair.

First I make some fake data

matrix={
{6,1,1,1,1,1},
{5,2,2,2,2,2},
{4,3,3,3,3,3},
{60,4,4,4,4,4},
{61,5,5,5,5,5},
{62,6,6,6,6,6},
{190,7,7,7,7,7}};

And then use Partition to pair up the items, Select to decide if each pair is acceptable and finally Map[First to keep the first item and discard the second item of each pair.

ascending=Map[First,Select[Partition[matrix,2,1],#[[2,1]]>#[[1,1]]&&60<=#[[1,1]]<=160&]]

the result from that is

{{60,4,4,4,4,4},{61,5,5,5,5,5},{62,6,6,6,6,6}}

There is a lot going on in that for someone who is new. See if you can look up each of those functions in the help system. # and & are parts of "anonymous functions", functions that don't even get given a name and are just used in that line and perhaps never again.

A different way of writing that might be easier for someone new to understand.

test[{{x1_,_,_,_,_,_},{x2_,_,_,_,_,_}}]:=x1<x2&&60<=x1<=160;
ascending=Map[First,Select[Partition[matrix,2,1],test]]

Try this on your actual data and see if it works. Make sure you have removed enough extra layers of {} that imported csv files sometimes have.

$\endgroup$
  • $\begingroup$ Hi Bill, I was so much into making my code work, I completely forgot to thank you, my sincere apologies!!!!! It worked perfectly. Thank you so very much again and Take Care man! $\endgroup$ – NAZ001 Oct 9 '20 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.