Problems in dealing with sublists
I have a list made by several sublists of two different kinds: the first kind of sublist has just one element, the second kind has three elements. I also have two functions: I would like to apply the first function to the elements of those sublists that have only one component, and the other function to the elements of those sublists with three components.
To give you some context:
I am interested in studying the points of intersection between a function and a constant function, for different values of the constant function itself (that I called $l$).
To see the function, run the following code:
cdf1[x_] = CDF[NormalDistribution[1, 1], x];
pdf1[x_] = PDF[NormalDistribution[1, 1], x];
cdfm1[x_] = cdf1[x]^20;
pdfm1[x_] = cdfm1'[x];
cdf[x_] = CDF[NormalDistribution[0, 1], x];
pdf[x_] = PDF[NormalDistribution[0, 1], x];
cdfm[x_] = cdf[x]^20;
pdfm[x_] = cdfm'[x];
mix1[x_, h_] = h pdf1[x] + (1 - h) pdfm1[x];
mix[x_, h_] = h pdf[x] + (1 - h) pdfm [x];
ratio[x_, h_] = mix1[x, h]/mix[x, h];
Manipulate[Plot[{ratio[x, 0.4], l}, {x, -6, 7}, PlotRange -> 1.5], {l, 0, 1.5}]
As you can see, for very low or very high values of $l$ there is only one point of intersection, for all the other values of $l$ there are three points of intersection.
I obtained all the points of intersection for different values of $l$ with this code:
sol = Solve[ratio[x, 0.4] == l && (-10 < x < 10), x]
tab = Table[x /. sol, {l, 0.1, 1.5, 0.1}]
{{-1.80259}, {-1.10944}, {-0.703973, 1.52579, 1.7873}, {-0.416291,
1.32432, 1.9424}, {-0.193145, 1.21091, 2.02258}, {-0.0107771,
1.12506, 2.08055}, {0.143772, 1.0518, 2.12697}, {0.279357, 0.983264,
2.16616}, {0.404907, 0.912193, 2.20033}, {0.538168, 0.822835,
2.23079}, {2.2584}, {2.28373}, {2.30719}, {2.3291}, {2.34968}}
So we have a list made of sublists of two kinds: either {a} or {b,c,d}, with $b<c<d$.
I would like to obtain a list that displays the value 1-cdf[a]
if the sublist is made by $a$ only, and that displays the value cdf[c]-cdf[b]+1-cdf[d]
if the sublist is made of three element.
So, for example, in the case of the list I provided, I would like to get another list like this:
{1-cdf[-1.80259], 1-cdf[-1.10944], cdf[1.52579]-cdf[-0.703973]+1-cdf[1.7873]...}
with values instead of the expression, of course.
Thank you very much for any help you can provide!
list-manipulation
add a comment |
I have a list made by several sublists of two different kinds: the first kind of sublist has just one element, the second kind has three elements. I also have two functions: I would like to apply the first function to the elements of those sublists that have only one component, and the other function to the elements of those sublists with three components.
To give you some context:
I am interested in studying the points of intersection between a function and a constant function, for different values of the constant function itself (that I called $l$).
To see the function, run the following code:
cdf1[x_] = CDF[NormalDistribution[1, 1], x];
pdf1[x_] = PDF[NormalDistribution[1, 1], x];
cdfm1[x_] = cdf1[x]^20;
pdfm1[x_] = cdfm1'[x];
cdf[x_] = CDF[NormalDistribution[0, 1], x];
pdf[x_] = PDF[NormalDistribution[0, 1], x];
cdfm[x_] = cdf[x]^20;
pdfm[x_] = cdfm'[x];
mix1[x_, h_] = h pdf1[x] + (1 - h) pdfm1[x];
mix[x_, h_] = h pdf[x] + (1 - h) pdfm [x];
ratio[x_, h_] = mix1[x, h]/mix[x, h];
Manipulate[Plot[{ratio[x, 0.4], l}, {x, -6, 7}, PlotRange -> 1.5], {l, 0, 1.5}]
As you can see, for very low or very high values of $l$ there is only one point of intersection, for all the other values of $l$ there are three points of intersection.
I obtained all the points of intersection for different values of $l$ with this code:
sol = Solve[ratio[x, 0.4] == l && (-10 < x < 10), x]
tab = Table[x /. sol, {l, 0.1, 1.5, 0.1}]
{{-1.80259}, {-1.10944}, {-0.703973, 1.52579, 1.7873}, {-0.416291,
1.32432, 1.9424}, {-0.193145, 1.21091, 2.02258}, {-0.0107771,
1.12506, 2.08055}, {0.143772, 1.0518, 2.12697}, {0.279357, 0.983264,
2.16616}, {0.404907, 0.912193, 2.20033}, {0.538168, 0.822835,
2.23079}, {2.2584}, {2.28373}, {2.30719}, {2.3291}, {2.34968}}
So we have a list made of sublists of two kinds: either {a} or {b,c,d}, with $b<c<d$.
I would like to obtain a list that displays the value 1-cdf[a]
if the sublist is made by $a$ only, and that displays the value cdf[c]-cdf[b]+1-cdf[d]
if the sublist is made of three element.
So, for example, in the case of the list I provided, I would like to get another list like this:
{1-cdf[-1.80259], 1-cdf[-1.10944], cdf[1.52579]-cdf[-0.703973]+1-cdf[1.7873]...}
with values instead of the expression, of course.
Thank you very much for any help you can provide!
list-manipulation
add a comment |
I have a list made by several sublists of two different kinds: the first kind of sublist has just one element, the second kind has three elements. I also have two functions: I would like to apply the first function to the elements of those sublists that have only one component, and the other function to the elements of those sublists with three components.
To give you some context:
I am interested in studying the points of intersection between a function and a constant function, for different values of the constant function itself (that I called $l$).
To see the function, run the following code:
cdf1[x_] = CDF[NormalDistribution[1, 1], x];
pdf1[x_] = PDF[NormalDistribution[1, 1], x];
cdfm1[x_] = cdf1[x]^20;
pdfm1[x_] = cdfm1'[x];
cdf[x_] = CDF[NormalDistribution[0, 1], x];
pdf[x_] = PDF[NormalDistribution[0, 1], x];
cdfm[x_] = cdf[x]^20;
pdfm[x_] = cdfm'[x];
mix1[x_, h_] = h pdf1[x] + (1 - h) pdfm1[x];
mix[x_, h_] = h pdf[x] + (1 - h) pdfm [x];
ratio[x_, h_] = mix1[x, h]/mix[x, h];
Manipulate[Plot[{ratio[x, 0.4], l}, {x, -6, 7}, PlotRange -> 1.5], {l, 0, 1.5}]
As you can see, for very low or very high values of $l$ there is only one point of intersection, for all the other values of $l$ there are three points of intersection.
I obtained all the points of intersection for different values of $l$ with this code:
sol = Solve[ratio[x, 0.4] == l && (-10 < x < 10), x]
tab = Table[x /. sol, {l, 0.1, 1.5, 0.1}]
{{-1.80259}, {-1.10944}, {-0.703973, 1.52579, 1.7873}, {-0.416291,
1.32432, 1.9424}, {-0.193145, 1.21091, 2.02258}, {-0.0107771,
1.12506, 2.08055}, {0.143772, 1.0518, 2.12697}, {0.279357, 0.983264,
2.16616}, {0.404907, 0.912193, 2.20033}, {0.538168, 0.822835,
2.23079}, {2.2584}, {2.28373}, {2.30719}, {2.3291}, {2.34968}}
So we have a list made of sublists of two kinds: either {a} or {b,c,d}, with $b<c<d$.
I would like to obtain a list that displays the value 1-cdf[a]
if the sublist is made by $a$ only, and that displays the value cdf[c]-cdf[b]+1-cdf[d]
if the sublist is made of three element.
So, for example, in the case of the list I provided, I would like to get another list like this:
{1-cdf[-1.80259], 1-cdf[-1.10944], cdf[1.52579]-cdf[-0.703973]+1-cdf[1.7873]...}
with values instead of the expression, of course.
Thank you very much for any help you can provide!
list-manipulation
I have a list made by several sublists of two different kinds: the first kind of sublist has just one element, the second kind has three elements. I also have two functions: I would like to apply the first function to the elements of those sublists that have only one component, and the other function to the elements of those sublists with three components.
To give you some context:
I am interested in studying the points of intersection between a function and a constant function, for different values of the constant function itself (that I called $l$).
To see the function, run the following code:
cdf1[x_] = CDF[NormalDistribution[1, 1], x];
pdf1[x_] = PDF[NormalDistribution[1, 1], x];
cdfm1[x_] = cdf1[x]^20;
pdfm1[x_] = cdfm1'[x];
cdf[x_] = CDF[NormalDistribution[0, 1], x];
pdf[x_] = PDF[NormalDistribution[0, 1], x];
cdfm[x_] = cdf[x]^20;
pdfm[x_] = cdfm'[x];
mix1[x_, h_] = h pdf1[x] + (1 - h) pdfm1[x];
mix[x_, h_] = h pdf[x] + (1 - h) pdfm [x];
ratio[x_, h_] = mix1[x, h]/mix[x, h];
Manipulate[Plot[{ratio[x, 0.4], l}, {x, -6, 7}, PlotRange -> 1.5], {l, 0, 1.5}]
As you can see, for very low or very high values of $l$ there is only one point of intersection, for all the other values of $l$ there are three points of intersection.
I obtained all the points of intersection for different values of $l$ with this code:
sol = Solve[ratio[x, 0.4] == l && (-10 < x < 10), x]
tab = Table[x /. sol, {l, 0.1, 1.5, 0.1}]
{{-1.80259}, {-1.10944}, {-0.703973, 1.52579, 1.7873}, {-0.416291,
1.32432, 1.9424}, {-0.193145, 1.21091, 2.02258}, {-0.0107771,
1.12506, 2.08055}, {0.143772, 1.0518, 2.12697}, {0.279357, 0.983264,
2.16616}, {0.404907, 0.912193, 2.20033}, {0.538168, 0.822835,
2.23079}, {2.2584}, {2.28373}, {2.30719}, {2.3291}, {2.34968}}
So we have a list made of sublists of two kinds: either {a} or {b,c,d}, with $b<c<d$.
I would like to obtain a list that displays the value 1-cdf[a]
if the sublist is made by $a$ only, and that displays the value cdf[c]-cdf[b]+1-cdf[d]
if the sublist is made of three element.
So, for example, in the case of the list I provided, I would like to get another list like this:
{1-cdf[-1.80259], 1-cdf[-1.10944], cdf[1.52579]-cdf[-0.703973]+1-cdf[1.7873]...}
with values instead of the expression, of course.
Thank you very much for any help you can provide!
list-manipulation
list-manipulation
asked Dec 28 '18 at 17:22
Api
537
537
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can achieve what you're after creating a function and overloading it for 1 and 3 arguments:
func[a_]:=1-cdf[a]
func[b_,c_,d_]:=cdf[c]-cdf[b]+1-cdf[d]
Now, Apply
ing func
to your list gives the result:
func @@@ tab
(* {0.964273, 0.866379, 0.732689, 0.594747, 0.485171, 0.392755, 0.313105, 0.242396,
0.175831, 0.102777, 0.0119603, 0.0111937, 0.0105221, 0.00992698, 0.00939481} *)
You can also use Replace
to achieve the same without an additional function:
Replace[
tab,
{
{a_} :> 1 - cdf[a],
{b_, c_, d_} :> cdf[c] - cdf[b] + 1 - cdf[d]
},
1
]
Thank you very much!
– Api
Dec 28 '18 at 17:53
add a comment |
A basic approach to your problem
fun[list_] :=
Table[Which[Length[list[[i]]] == 1, First[1 - cdf[list[[i]]]],
Length[list[[i]]] == 3,
cdf[list[[i, 2]]] - cdf[list[[i, 1]]] + 1 - cdf[list[[i, 3]]]], {i,
1, Length[tab]}]
fun[tab]
(*{0.964274, 0.86638, 0.732689, 0.594747, 0.485171, 0.392755, 0.313106,
0.242396, 0.175831, 0.102777, 0.0119604, 0.0111937, 0.0105221,0.00992688, 0.00939478}*)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188507%2fproblems-in-dealing-with-sublists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can achieve what you're after creating a function and overloading it for 1 and 3 arguments:
func[a_]:=1-cdf[a]
func[b_,c_,d_]:=cdf[c]-cdf[b]+1-cdf[d]
Now, Apply
ing func
to your list gives the result:
func @@@ tab
(* {0.964273, 0.866379, 0.732689, 0.594747, 0.485171, 0.392755, 0.313105, 0.242396,
0.175831, 0.102777, 0.0119603, 0.0111937, 0.0105221, 0.00992698, 0.00939481} *)
You can also use Replace
to achieve the same without an additional function:
Replace[
tab,
{
{a_} :> 1 - cdf[a],
{b_, c_, d_} :> cdf[c] - cdf[b] + 1 - cdf[d]
},
1
]
Thank you very much!
– Api
Dec 28 '18 at 17:53
add a comment |
You can achieve what you're after creating a function and overloading it for 1 and 3 arguments:
func[a_]:=1-cdf[a]
func[b_,c_,d_]:=cdf[c]-cdf[b]+1-cdf[d]
Now, Apply
ing func
to your list gives the result:
func @@@ tab
(* {0.964273, 0.866379, 0.732689, 0.594747, 0.485171, 0.392755, 0.313105, 0.242396,
0.175831, 0.102777, 0.0119603, 0.0111937, 0.0105221, 0.00992698, 0.00939481} *)
You can also use Replace
to achieve the same without an additional function:
Replace[
tab,
{
{a_} :> 1 - cdf[a],
{b_, c_, d_} :> cdf[c] - cdf[b] + 1 - cdf[d]
},
1
]
Thank you very much!
– Api
Dec 28 '18 at 17:53
add a comment |
You can achieve what you're after creating a function and overloading it for 1 and 3 arguments:
func[a_]:=1-cdf[a]
func[b_,c_,d_]:=cdf[c]-cdf[b]+1-cdf[d]
Now, Apply
ing func
to your list gives the result:
func @@@ tab
(* {0.964273, 0.866379, 0.732689, 0.594747, 0.485171, 0.392755, 0.313105, 0.242396,
0.175831, 0.102777, 0.0119603, 0.0111937, 0.0105221, 0.00992698, 0.00939481} *)
You can also use Replace
to achieve the same without an additional function:
Replace[
tab,
{
{a_} :> 1 - cdf[a],
{b_, c_, d_} :> cdf[c] - cdf[b] + 1 - cdf[d]
},
1
]
You can achieve what you're after creating a function and overloading it for 1 and 3 arguments:
func[a_]:=1-cdf[a]
func[b_,c_,d_]:=cdf[c]-cdf[b]+1-cdf[d]
Now, Apply
ing func
to your list gives the result:
func @@@ tab
(* {0.964273, 0.866379, 0.732689, 0.594747, 0.485171, 0.392755, 0.313105, 0.242396,
0.175831, 0.102777, 0.0119603, 0.0111937, 0.0105221, 0.00992698, 0.00939481} *)
You can also use Replace
to achieve the same without an additional function:
Replace[
tab,
{
{a_} :> 1 - cdf[a],
{b_, c_, d_} :> cdf[c] - cdf[b] + 1 - cdf[d]
},
1
]
answered Dec 28 '18 at 17:40
Lukas Lang
6,1131928
6,1131928
Thank you very much!
– Api
Dec 28 '18 at 17:53
add a comment |
Thank you very much!
– Api
Dec 28 '18 at 17:53
Thank you very much!
– Api
Dec 28 '18 at 17:53
Thank you very much!
– Api
Dec 28 '18 at 17:53
add a comment |
A basic approach to your problem
fun[list_] :=
Table[Which[Length[list[[i]]] == 1, First[1 - cdf[list[[i]]]],
Length[list[[i]]] == 3,
cdf[list[[i, 2]]] - cdf[list[[i, 1]]] + 1 - cdf[list[[i, 3]]]], {i,
1, Length[tab]}]
fun[tab]
(*{0.964274, 0.86638, 0.732689, 0.594747, 0.485171, 0.392755, 0.313106,
0.242396, 0.175831, 0.102777, 0.0119604, 0.0111937, 0.0105221,0.00992688, 0.00939478}*)
add a comment |
A basic approach to your problem
fun[list_] :=
Table[Which[Length[list[[i]]] == 1, First[1 - cdf[list[[i]]]],
Length[list[[i]]] == 3,
cdf[list[[i, 2]]] - cdf[list[[i, 1]]] + 1 - cdf[list[[i, 3]]]], {i,
1, Length[tab]}]
fun[tab]
(*{0.964274, 0.86638, 0.732689, 0.594747, 0.485171, 0.392755, 0.313106,
0.242396, 0.175831, 0.102777, 0.0119604, 0.0111937, 0.0105221,0.00992688, 0.00939478}*)
add a comment |
A basic approach to your problem
fun[list_] :=
Table[Which[Length[list[[i]]] == 1, First[1 - cdf[list[[i]]]],
Length[list[[i]]] == 3,
cdf[list[[i, 2]]] - cdf[list[[i, 1]]] + 1 - cdf[list[[i, 3]]]], {i,
1, Length[tab]}]
fun[tab]
(*{0.964274, 0.86638, 0.732689, 0.594747, 0.485171, 0.392755, 0.313106,
0.242396, 0.175831, 0.102777, 0.0119604, 0.0111937, 0.0105221,0.00992688, 0.00939478}*)
A basic approach to your problem
fun[list_] :=
Table[Which[Length[list[[i]]] == 1, First[1 - cdf[list[[i]]]],
Length[list[[i]]] == 3,
cdf[list[[i, 2]]] - cdf[list[[i, 1]]] + 1 - cdf[list[[i, 3]]]], {i,
1, Length[tab]}]
fun[tab]
(*{0.964274, 0.86638, 0.732689, 0.594747, 0.485171, 0.392755, 0.313106,
0.242396, 0.175831, 0.102777, 0.0119604, 0.0111937, 0.0105221,0.00992688, 0.00939478}*)
answered Dec 28 '18 at 17:53
Hubble07
2,892720
2,892720
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188507%2fproblems-in-dealing-with-sublists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown