Message Boards Message Boards

0
|
4819 Views
|
13 Replies
|
5 Total Likes
View groups...
Share
Share this post:
GROUPS:

Last is empty

Posted 10 years ago

Hi I have an assigment where I need to calculate a probability for different cases and plot it I alredy plot it but I also have to make a table with the maximum probability and its position on the plot, the probability is done however I have problems in the positionof the maximum for some reason it doesnt let me use take to find the value distribution = Table[bin1[50, #, k], {k, 0, 50}] & /@ Range[0, .9, .1]; that is what i have to plot prob = Max[Take[distribution, {#}]] & /@ Range[1, 10] thats how i took the probability max = Last[ Last[Position[distribution, Take[prob, {#} & Range[1, 10]]]]] and thats how i intended to take the maximum number but it says last is empty

POSTED BY: Hansel Montuffar
13 Replies

I just can not resist ...
How about:

In[1]:= Clear[maxList]

In[2]:= maxList = {{{1, 1}}, {{2, 6}}, {{3, 11}}, {{4, 16}}, {{5, 
     21}}, {{6, 26}}, {{7, 31}}, {{8, 36}}, {{9, 41}}, {{10, 46}}};

In[3]:= Last @@@ maxList

Out[3]= {1, 6, 11, 16, 21, 26, 31, 36, 41, 46}
POSTED BY: Henrik Schachner

One can also play a bit with Transpose

In[1]:= Clear[maxList]
maxList = {{{1, 1}}, {{2, 6}}, {{3, 11}}, {{4, 16}}, {{5, 21}}, {{6, 26}}, {{7, 31}}, {{8, 36}}, {{9, 41}}, {{10, 46}}};

In[13]:= Transpose[maxList, {2, 1, 3}]
Out[13]= {{{1, 1}, {2, 6}, {3, 11}, {4, 16}, {5, 21}, {6, 26}, {7, 31}, {8, 36}, {9, 41}, {10, 46}}}

In[14]:= Transpose[maxList, {2, 3, 1}]
Out[14]= {{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}}, {{1}, {6}, {11}, {16}, {21}, {26}, {31}, {36}, {41}, {46}}}

In[15]:= Transpose[maxList, {3, 2, 1}]
Out[15]= {{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, {{1, 6, 11, 16, 21, 26, 31, 36, 41, 46}}}

so it is

In[16]:= Transpose[maxList, {3, 2, 1}][[-1, 1]]
Out[16]= {1, 6, 11, 16, 21, 26, 31, 36, 41, 46}
POSTED BY: Udo Krause

Thank you! I didnt think of using Flatten because I still havent learned in school it really helped me out

POSTED BY: Hansel Montuffar

Understanding lists and list operations are VERY important in Mathematica. Here's a way to do what you're looking for, but I challenge you to learn why this works:

In[72]:= maxList= {{{1, 1}}, {{2, 6}}, {{3, 11}}, {{4, 16}}, {{5, 
     21}}, {{6, 26}}, {{7, 31}}, {{8, 36}}, {{9, 41}}, {{10, 46}}};
Last /@ Flatten[maxList, 1]

Out[73]= {1, 6, 11, 16, 21, 26, 31, 36, 41, 46}

Have fun!

POSTED BY: Chad Knutson

Thanks this really helped me a lot but Im tryingo to make a list with all the second results (1,6,11,16,21,26.,etc) not just the last one

POSTED BY: Hansel Montuffar

Excellent, your full code helps a lot. The problem is with how you are using Take. I prefer using [[.. ]] rather than take, but we can make either of them work. First, using take:

In[17]:= Take[prob, {#}] & /@ Range[10]

Out[17]= {{1.}, {0.184925}, {0.139819}, {0.122347}, {0.114559},  {0.112275}, {0.114559}, {0.122347}, {0.139819}, {0.184925}}

Note that we now have a list of lists. So if we use this directly in Position, it will be looking for {1.} instead of 1., which I assume is not what you want. To make this work correctly, you'll want to extract the number from the list using First:

Position[distribution, First@Take[prob, {#}]] & /@ Range[10]

But let's use the [[..]] notation because I like it.

In[21]:= maxList = Position[distribution, prob[[#]]] & /@ Range[10]

Out[21]= {{{1, 1}}, {{2, 6}}, {{3, 11}}, {{4, 16}}, {{5, 21}}, {{6, 26}}, {{7, 31}}, {{8, 36}}, {{9, 41}}, {{10, 46}}}

In[22]:= Last[Last[maxList]]

Out[22]= {10, 46}

And that, I hope, is the result that you were looking for. Is it?

POSTED BY: Chad Knutson

Let me be clearer

<Clear[bin1]
bin1[n_, p_, k_] := 
 Module[{prob = (1 - p)^n, i}, 
  Do[prob = (((n - i + 1)/i) (p/(1 - p))) prob, {i, k}]; prob]>

Thats the probability formula im using

 <distribution = 
      Table[bin1[50, #, k], {k, 0, 50}] & /@ Range[0, .9, .1];>

thats what I had to plot

ListPlot[distribution, GridLines -> Automatic,
     PlotLegends -> {"0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7"
       , "0.8", "0.9"}, Joined -> True, PlotRange -> All]

thats how i plotted

 prob = Max[Take[distribution, {#}]] & /@ Range[1, 10]

that is my function to find the maximum values

max= Last[ Last[Position[distribution, Take[prob, {#} & Range[1, 10]]]]]

Thats the function who doesnt work where I tried to find the position of the maximum values

POSTED BY: Hansel Montuffar

Your maxList has the same error as my max

POSTED BY: Hansel Montuffar

What did you try to do?

Did you change your input to what I typed? What is the result for maxList?

POSTED BY: Chad Knutson

Yes i tried to do that im creating a TableForm with the results however I want to make a list of them in maxList but it says that Last is empty and I have no idea why

POSTED BY: Hansel Montuffar

Oops! I didn't read your code carefully.

As an aside, to make your posts easier to read, try using the <> button for inputting code.

Since I don't know exactly what you're trying to do, this is my guess for what you meant to do in the last line:

maxList = Position[distribution, Take[prob, {#}] &/@ Range[1, 10]]

Is that correct?

POSTED BY: Chad Knutson

Take::normal: "Nonatomic expression expected at position 1 in Take[prob,{{#1}&,2 ({#1}&),3 ({#1}&),4 ({#1}&),5 ({#1}&),6 ({#1}&),7 ({#1}&),8 ({#1}&),9 ({#1}&),10 ({#1}&)}]. \!(*ButtonBox[\">>\", Appearance->{Automatic, None}, BaseStyle->\"Link\", ButtonData:>\"paclet:ref/message/General/normal\", ButtonNote->\"Take::normal\"])"

I got that error

POSTED BY: Hansel Montuffar

As a first step, try not including the Last evalutions in the last line of code:

maxList = Position[distribution, Take[prob, {#} & Range[1, 10]]]

What does this result look like? Last works on lists, so you can take a look at the dimensions of your result:

Dimensions[maxList]

If this is not a list, or is just a 1 dimensional list, then evaluating Last[Last[maxList]] will return an empty result.

POSTED BY: Chad Knutson
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