Finding the index of a specific element in a list
$begingroup$
I have searched everywhere to find an answer for this and have come up with nothing!
I have a data file containing two columns, one with wavelengths in nanometer (some with decimal digits, others just with a decimal point) and the other with spectral irradiance values.
I am trying to make a new table that takes as the first column all the wavelengths and in the second column I divide the spectral values by photon energy to get photon flux. However, in this new table I want to stop at a certain index i where the wavelength corresponds to a specific photon energy that I calculated. This is the code I use. It works.
Here is a small portion of the data table:
{280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5,
SSpecUsed = Drop[Table[{SSpecWavelength[[i]],SSpecIntensity[[i]]/Eph[SSpecWavelength[[i]]]*Subscript[En, G, Si]*q /. numval}, {i, 0, 949, 1}], 1];
My issue is that I manually found the index (949) at which the wavelength is 1107. nm. In a later exercise I need to integrate over this data up to different indices (so for example integrating all the way up to wavelength 2000 and in the next step only up to 1999). So I tried replacing the 949 value by:
Position[SSpecWavelength, Subscript[[Lambda], G][Eg_]]/.numval
So basically I try using Position to find the index at which the element in the list is equal to the wavelenght that I calculate based on an energy value. When I run it I get empty curly brackets. I even tried this:
Position[SSpecWavelength, 1107.]
and I still get empty curly brackets. I checked and I know for a fact that the element at index 949 is equal to 1107. so why does it not return me the index?
Is there another (better/simpler) way to accomplish what I want?
Thanks in advance, hope the problem is clear!
list-manipulation numerical-value
$endgroup$
|
show 3 more comments
$begingroup$
I have searched everywhere to find an answer for this and have come up with nothing!
I have a data file containing two columns, one with wavelengths in nanometer (some with decimal digits, others just with a decimal point) and the other with spectral irradiance values.
I am trying to make a new table that takes as the first column all the wavelengths and in the second column I divide the spectral values by photon energy to get photon flux. However, in this new table I want to stop at a certain index i where the wavelength corresponds to a specific photon energy that I calculated. This is the code I use. It works.
Here is a small portion of the data table:
{280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5,
SSpecUsed = Drop[Table[{SSpecWavelength[[i]],SSpecIntensity[[i]]/Eph[SSpecWavelength[[i]]]*Subscript[En, G, Si]*q /. numval}, {i, 0, 949, 1}], 1];
My issue is that I manually found the index (949) at which the wavelength is 1107. nm. In a later exercise I need to integrate over this data up to different indices (so for example integrating all the way up to wavelength 2000 and in the next step only up to 1999). So I tried replacing the 949 value by:
Position[SSpecWavelength, Subscript[[Lambda], G][Eg_]]/.numval
So basically I try using Position to find the index at which the element in the list is equal to the wavelenght that I calculate based on an energy value. When I run it I get empty curly brackets. I even tried this:
Position[SSpecWavelength, 1107.]
and I still get empty curly brackets. I checked and I know for a fact that the element at index 949 is equal to 1107. so why does it not return me the index?
Is there another (better/simpler) way to accomplish what I want?
Thanks in advance, hope the problem is clear!
list-manipulation numerical-value
$endgroup$
$begingroup$
Would you please provide a small portion of the data table as an example? That would make live much easier for us.
$endgroup$
– Henrik Schumacher
7 hours ago
1
$begingroup$
You may try something likeFirstPosition[SSpecWavelength, _?(# > 1107. &)]
to find the first position with a value larger than1107.
.
$endgroup$
– Henrik Schumacher
7 hours ago
1
$begingroup$
In generalPosition
may take floating point numbers more literate than you. The value from the list could be1107.0000001
or so but only the leading digits are shown. This difference may be small, butPosition
won't match1107.
with1107.0000001
.
$endgroup$
– Henrik Schumacher
7 hours ago
$begingroup$
FirstPosition works actually. But why does Position not work? The element in the list is exactly 1107.
$endgroup$
– alonoid
7 hours ago
$begingroup$
How do I only use the returned index number without the curly brackets? How do I remove the brackets?
$endgroup$
– alonoid
7 hours ago
|
show 3 more comments
$begingroup$
I have searched everywhere to find an answer for this and have come up with nothing!
I have a data file containing two columns, one with wavelengths in nanometer (some with decimal digits, others just with a decimal point) and the other with spectral irradiance values.
I am trying to make a new table that takes as the first column all the wavelengths and in the second column I divide the spectral values by photon energy to get photon flux. However, in this new table I want to stop at a certain index i where the wavelength corresponds to a specific photon energy that I calculated. This is the code I use. It works.
Here is a small portion of the data table:
{280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5,
SSpecUsed = Drop[Table[{SSpecWavelength[[i]],SSpecIntensity[[i]]/Eph[SSpecWavelength[[i]]]*Subscript[En, G, Si]*q /. numval}, {i, 0, 949, 1}], 1];
My issue is that I manually found the index (949) at which the wavelength is 1107. nm. In a later exercise I need to integrate over this data up to different indices (so for example integrating all the way up to wavelength 2000 and in the next step only up to 1999). So I tried replacing the 949 value by:
Position[SSpecWavelength, Subscript[[Lambda], G][Eg_]]/.numval
So basically I try using Position to find the index at which the element in the list is equal to the wavelenght that I calculate based on an energy value. When I run it I get empty curly brackets. I even tried this:
Position[SSpecWavelength, 1107.]
and I still get empty curly brackets. I checked and I know for a fact that the element at index 949 is equal to 1107. so why does it not return me the index?
Is there another (better/simpler) way to accomplish what I want?
Thanks in advance, hope the problem is clear!
list-manipulation numerical-value
$endgroup$
I have searched everywhere to find an answer for this and have come up with nothing!
I have a data file containing two columns, one with wavelengths in nanometer (some with decimal digits, others just with a decimal point) and the other with spectral irradiance values.
I am trying to make a new table that takes as the first column all the wavelengths and in the second column I divide the spectral values by photon energy to get photon flux. However, in this new table I want to stop at a certain index i where the wavelength corresponds to a specific photon energy that I calculated. This is the code I use. It works.
Here is a small portion of the data table:
{280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5,
SSpecUsed = Drop[Table[{SSpecWavelength[[i]],SSpecIntensity[[i]]/Eph[SSpecWavelength[[i]]]*Subscript[En, G, Si]*q /. numval}, {i, 0, 949, 1}], 1];
My issue is that I manually found the index (949) at which the wavelength is 1107. nm. In a later exercise I need to integrate over this data up to different indices (so for example integrating all the way up to wavelength 2000 and in the next step only up to 1999). So I tried replacing the 949 value by:
Position[SSpecWavelength, Subscript[[Lambda], G][Eg_]]/.numval
So basically I try using Position to find the index at which the element in the list is equal to the wavelenght that I calculate based on an energy value. When I run it I get empty curly brackets. I even tried this:
Position[SSpecWavelength, 1107.]
and I still get empty curly brackets. I checked and I know for a fact that the element at index 949 is equal to 1107. so why does it not return me the index?
Is there another (better/simpler) way to accomplish what I want?
Thanks in advance, hope the problem is clear!
list-manipulation numerical-value
list-manipulation numerical-value
edited 6 hours ago
m_goldberg
86.7k872196
86.7k872196
asked 7 hours ago
alonoidalonoid
262
262
$begingroup$
Would you please provide a small portion of the data table as an example? That would make live much easier for us.
$endgroup$
– Henrik Schumacher
7 hours ago
1
$begingroup$
You may try something likeFirstPosition[SSpecWavelength, _?(# > 1107. &)]
to find the first position with a value larger than1107.
.
$endgroup$
– Henrik Schumacher
7 hours ago
1
$begingroup$
In generalPosition
may take floating point numbers more literate than you. The value from the list could be1107.0000001
or so but only the leading digits are shown. This difference may be small, butPosition
won't match1107.
with1107.0000001
.
$endgroup$
– Henrik Schumacher
7 hours ago
$begingroup$
FirstPosition works actually. But why does Position not work? The element in the list is exactly 1107.
$endgroup$
– alonoid
7 hours ago
$begingroup$
How do I only use the returned index number without the curly brackets? How do I remove the brackets?
$endgroup$
– alonoid
7 hours ago
|
show 3 more comments
$begingroup$
Would you please provide a small portion of the data table as an example? That would make live much easier for us.
$endgroup$
– Henrik Schumacher
7 hours ago
1
$begingroup$
You may try something likeFirstPosition[SSpecWavelength, _?(# > 1107. &)]
to find the first position with a value larger than1107.
.
$endgroup$
– Henrik Schumacher
7 hours ago
1
$begingroup$
In generalPosition
may take floating point numbers more literate than you. The value from the list could be1107.0000001
or so but only the leading digits are shown. This difference may be small, butPosition
won't match1107.
with1107.0000001
.
$endgroup$
– Henrik Schumacher
7 hours ago
$begingroup$
FirstPosition works actually. But why does Position not work? The element in the list is exactly 1107.
$endgroup$
– alonoid
7 hours ago
$begingroup$
How do I only use the returned index number without the curly brackets? How do I remove the brackets?
$endgroup$
– alonoid
7 hours ago
$begingroup$
Would you please provide a small portion of the data table as an example? That would make live much easier for us.
$endgroup$
– Henrik Schumacher
7 hours ago
$begingroup$
Would you please provide a small portion of the data table as an example? That would make live much easier for us.
$endgroup$
– Henrik Schumacher
7 hours ago
1
1
$begingroup$
You may try something like
FirstPosition[SSpecWavelength, _?(# > 1107. &)]
to find the first position with a value larger than 1107.
.$endgroup$
– Henrik Schumacher
7 hours ago
$begingroup$
You may try something like
FirstPosition[SSpecWavelength, _?(# > 1107. &)]
to find the first position with a value larger than 1107.
.$endgroup$
– Henrik Schumacher
7 hours ago
1
1
$begingroup$
In general
Position
may take floating point numbers more literate than you. The value from the list could be 1107.0000001
or so but only the leading digits are shown. This difference may be small, but Position
won't match 1107.
with 1107.0000001
.$endgroup$
– Henrik Schumacher
7 hours ago
$begingroup$
In general
Position
may take floating point numbers more literate than you. The value from the list could be 1107.0000001
or so but only the leading digits are shown. This difference may be small, but Position
won't match 1107.
with 1107.0000001
.$endgroup$
– Henrik Schumacher
7 hours ago
$begingroup$
FirstPosition works actually. But why does Position not work? The element in the list is exactly 1107.
$endgroup$
– alonoid
7 hours ago
$begingroup$
FirstPosition works actually. But why does Position not work? The element in the list is exactly 1107.
$endgroup$
– alonoid
7 hours ago
$begingroup$
How do I only use the returned index number without the curly brackets? How do I remove the brackets?
$endgroup$
– alonoid
7 hours ago
$begingroup$
How do I only use the returned index number without the curly brackets? How do I remove the brackets?
$endgroup$
– alonoid
7 hours ago
|
show 3 more comments
1 Answer
1
active
oldest
votes
$begingroup$
Given
data = {280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5};
you could use
data[[;; Position[data, 282.][[1, 1]]]]
or
data[[;; FirstPosition[data, 282.][[1]]]]
both return
{280., 280.5, 281., 281.5, 282.}
I recommend that you use the 2nd form; i.e., the one using FirstPosition
Now suppose data
was
data = {280., 280.5, 281., 281.5, 282.0000001, 282.5, 283., 283.5, 284., 284.5};
Then, although
data[[5]]
prints out as
282.
Position[data, 282.][[1, 1]]
generates the error message
To fix this you can write
Position[data, _?(Round[#, .1] == 282. &)]
{{5}}
The same change in its 2nd argument will fix the problem should FirstPostion
be used in place of Position
.
$endgroup$
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%2f192080%2ffinding-the-index-of-a-specific-element-in-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Given
data = {280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5};
you could use
data[[;; Position[data, 282.][[1, 1]]]]
or
data[[;; FirstPosition[data, 282.][[1]]]]
both return
{280., 280.5, 281., 281.5, 282.}
I recommend that you use the 2nd form; i.e., the one using FirstPosition
Now suppose data
was
data = {280., 280.5, 281., 281.5, 282.0000001, 282.5, 283., 283.5, 284., 284.5};
Then, although
data[[5]]
prints out as
282.
Position[data, 282.][[1, 1]]
generates the error message
To fix this you can write
Position[data, _?(Round[#, .1] == 282. &)]
{{5}}
The same change in its 2nd argument will fix the problem should FirstPostion
be used in place of Position
.
$endgroup$
add a comment |
$begingroup$
Given
data = {280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5};
you could use
data[[;; Position[data, 282.][[1, 1]]]]
or
data[[;; FirstPosition[data, 282.][[1]]]]
both return
{280., 280.5, 281., 281.5, 282.}
I recommend that you use the 2nd form; i.e., the one using FirstPosition
Now suppose data
was
data = {280., 280.5, 281., 281.5, 282.0000001, 282.5, 283., 283.5, 284., 284.5};
Then, although
data[[5]]
prints out as
282.
Position[data, 282.][[1, 1]]
generates the error message
To fix this you can write
Position[data, _?(Round[#, .1] == 282. &)]
{{5}}
The same change in its 2nd argument will fix the problem should FirstPostion
be used in place of Position
.
$endgroup$
add a comment |
$begingroup$
Given
data = {280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5};
you could use
data[[;; Position[data, 282.][[1, 1]]]]
or
data[[;; FirstPosition[data, 282.][[1]]]]
both return
{280., 280.5, 281., 281.5, 282.}
I recommend that you use the 2nd form; i.e., the one using FirstPosition
Now suppose data
was
data = {280., 280.5, 281., 281.5, 282.0000001, 282.5, 283., 283.5, 284., 284.5};
Then, although
data[[5]]
prints out as
282.
Position[data, 282.][[1, 1]]
generates the error message
To fix this you can write
Position[data, _?(Round[#, .1] == 282. &)]
{{5}}
The same change in its 2nd argument will fix the problem should FirstPostion
be used in place of Position
.
$endgroup$
Given
data = {280., 280.5, 281., 281.5, 282., 282.5, 283., 283.5, 284., 284.5};
you could use
data[[;; Position[data, 282.][[1, 1]]]]
or
data[[;; FirstPosition[data, 282.][[1]]]]
both return
{280., 280.5, 281., 281.5, 282.}
I recommend that you use the 2nd form; i.e., the one using FirstPosition
Now suppose data
was
data = {280., 280.5, 281., 281.5, 282.0000001, 282.5, 283., 283.5, 284., 284.5};
Then, although
data[[5]]
prints out as
282.
Position[data, 282.][[1, 1]]
generates the error message
To fix this you can write
Position[data, _?(Round[#, .1] == 282. &)]
{{5}}
The same change in its 2nd argument will fix the problem should FirstPostion
be used in place of Position
.
answered 6 hours ago
m_goldbergm_goldberg
86.7k872196
86.7k872196
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.
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%2f192080%2ffinding-the-index-of-a-specific-element-in-a-list%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
$begingroup$
Would you please provide a small portion of the data table as an example? That would make live much easier for us.
$endgroup$
– Henrik Schumacher
7 hours ago
1
$begingroup$
You may try something like
FirstPosition[SSpecWavelength, _?(# > 1107. &)]
to find the first position with a value larger than1107.
.$endgroup$
– Henrik Schumacher
7 hours ago
1
$begingroup$
In general
Position
may take floating point numbers more literate than you. The value from the list could be1107.0000001
or so but only the leading digits are shown. This difference may be small, butPosition
won't match1107.
with1107.0000001
.$endgroup$
– Henrik Schumacher
7 hours ago
$begingroup$
FirstPosition works actually. But why does Position not work? The element in the list is exactly 1107.
$endgroup$
– alonoid
7 hours ago
$begingroup$
How do I only use the returned index number without the curly brackets? How do I remove the brackets?
$endgroup$
– alonoid
7 hours ago