Comparing two arrays of unequal lengthHow to set a function as a parameter?Specifying an array with a...
Has any human ever had the choice to leave Earth permanently?
Why is Agricola named as such?
Difference between i++ and (i)++ in C
Use two 8s and two 3s to make the number 24
Why did the villain in the first Men in Black movie care about Earth's Cockroaches?
Comparing two arrays of unequal length
Can I make estimated tax payments instead of withholding from my paycheck?
Graph with overlapping labels
Is it possible to grant users sftp access without shell access? If yes, how is it implemented?
Flipping axis on a LogPlot
Do theoretical physics suggest that gravity is the exchange of gravitons or deformation/bending of spacetime?
What is the purpose of easy combat scenarios that don't need resource expenditure?
In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?
What are "industrial chops"?
Positioning node within rectangle Tikz
Can a hotel cancel a confirmed reservation?
Consequences of lack of rigour
How should I handle players who ignore the session zero agreement?
What is the difference between rolling more dice versus fewer dice?
How do I append a character to the end of every line in an Excel cell?
Can you tell from a blurry photo if focus was too close or too far?
Can we harness gravitational potential energy?
False written accusations not made public - is there law to cover this?
Nested word series [humans only]
Comparing two arrays of unequal length
How to set a function as a parameter?Specifying an array with a specific number of copies of two elementsMerging lists and adding common elementsAn efficient way to merge a pair of 4d arrays of non regular length listsList Manipulation on Array with subscript elementsSubtracting two plots from each other after shifting oneHow to use Compile to generate an $ntimes n$ array using $n$ vectorsFinding correct combination of functionsHow to assign values to symbolic arraysCompare arrays using custom pattern matching
$begingroup$
I have two arrays, say
array1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
array2 = {13, 4, 6, 20, 21};
I want to compare these two arrays by cycling array1
through all elements in array2
. The output should be the position in each array where they match a condition, where the condition array2[[m]] = array1[[n]]-1
is true. That is, the output should be:
output = {6,2}
Because in array2
, $5-1 = 4$. (array1[[6]] = 5
and array2[[2]] = 4
). So far, my code is
output = {Position[array1, #][[1, 1]],
Position[array2, #][[1, 1]]} & /@ (I have no idea)
Where the code on the left hand side of Map gives me the positions of a true condition. On the right hand side of Map, I'm not sure what to do.
To clarify a little bit, the code I want is similar to
output = {Position[array1, #][[1, 1]],
Position[array2, #][[1, 1]]} & /@ Intersection[array1, array2]
Except I want the position of the element when array2 = array1-1
instead of when array1 = array2
.
array conditional
New contributor
$endgroup$
add a comment |
$begingroup$
I have two arrays, say
array1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
array2 = {13, 4, 6, 20, 21};
I want to compare these two arrays by cycling array1
through all elements in array2
. The output should be the position in each array where they match a condition, where the condition array2[[m]] = array1[[n]]-1
is true. That is, the output should be:
output = {6,2}
Because in array2
, $5-1 = 4$. (array1[[6]] = 5
and array2[[2]] = 4
). So far, my code is
output = {Position[array1, #][[1, 1]],
Position[array2, #][[1, 1]]} & /@ (I have no idea)
Where the code on the left hand side of Map gives me the positions of a true condition. On the right hand side of Map, I'm not sure what to do.
To clarify a little bit, the code I want is similar to
output = {Position[array1, #][[1, 1]],
Position[array2, #][[1, 1]]} & /@ Intersection[array1, array2]
Except I want the position of the element when array2 = array1-1
instead of when array1 = array2
.
array conditional
New contributor
$endgroup$
add a comment |
$begingroup$
I have two arrays, say
array1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
array2 = {13, 4, 6, 20, 21};
I want to compare these two arrays by cycling array1
through all elements in array2
. The output should be the position in each array where they match a condition, where the condition array2[[m]] = array1[[n]]-1
is true. That is, the output should be:
output = {6,2}
Because in array2
, $5-1 = 4$. (array1[[6]] = 5
and array2[[2]] = 4
). So far, my code is
output = {Position[array1, #][[1, 1]],
Position[array2, #][[1, 1]]} & /@ (I have no idea)
Where the code on the left hand side of Map gives me the positions of a true condition. On the right hand side of Map, I'm not sure what to do.
To clarify a little bit, the code I want is similar to
output = {Position[array1, #][[1, 1]],
Position[array2, #][[1, 1]]} & /@ Intersection[array1, array2]
Except I want the position of the element when array2 = array1-1
instead of when array1 = array2
.
array conditional
New contributor
$endgroup$
I have two arrays, say
array1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
array2 = {13, 4, 6, 20, 21};
I want to compare these two arrays by cycling array1
through all elements in array2
. The output should be the position in each array where they match a condition, where the condition array2[[m]] = array1[[n]]-1
is true. That is, the output should be:
output = {6,2}
Because in array2
, $5-1 = 4$. (array1[[6]] = 5
and array2[[2]] = 4
). So far, my code is
output = {Position[array1, #][[1, 1]],
Position[array2, #][[1, 1]]} & /@ (I have no idea)
Where the code on the left hand side of Map gives me the positions of a true condition. On the right hand side of Map, I'm not sure what to do.
To clarify a little bit, the code I want is similar to
output = {Position[array1, #][[1, 1]],
Position[array2, #][[1, 1]]} & /@ Intersection[array1, array2]
Except I want the position of the element when array2 = array1-1
instead of when array1 = array2
.
array conditional
array conditional
New contributor
New contributor
edited 2 hours ago
Roman
2,055715
2,055715
New contributor
asked 3 hours ago
MetallicSilenceMetallicSilence
112
112
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
You can use Tuples
to construct all pairs of positions and use Select
to pick the pairs that satisfy your condition:
pairs = Tuples[{ Range[Length[array1]], Range[Length[array2]]}];
Select[pairs, array1[[#[[1]]]] - 1 == array2[[#[[2]]]] &]
{{6, 2}, {8, 3}}
You can also use Outer
as follows:
Join @@ Outer[If[array1[[#]] - 1 == array2[[#2]], {##}, Nothing] &,
Range[Length[array1]], Range[Length[array2]]]
{{6, 2}, {8, 3}}
Yet other ways: variations on Roman's method using Position
and Outer
combination:
Position[1] @ Outer[Subtract, array1, array2]
Position[True] @ Outer[Equal, array1 - 1, array2]
Position[{i_, i_}]@Outer[List, array1 - 1, array2]
{{6, 2}, {8, 3}}
$endgroup$
$begingroup$
I like yourOuter[Subtract, ...
way!
$endgroup$
– Roman
2 hours ago
$begingroup$
thank you @Roman. Didn't think of usingPosition
until i saw your answer.
$endgroup$
– kglr
2 hours ago
add a comment |
$begingroup$
This is a general solution for any condition between the elements of array1
and array2
:
Position[Outer[List, array1, array2], {i_, j_} /; j == i - 1]
{{6, 2}, {8, 3}}
@kglr's solutions are simpler than this when the condition is a difference as in the given problem.
$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
});
}
});
MetallicSilence is a new contributor. Be nice, and check out our Code of Conduct.
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%2f192349%2fcomparing-two-arrays-of-unequal-length%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
$begingroup$
You can use Tuples
to construct all pairs of positions and use Select
to pick the pairs that satisfy your condition:
pairs = Tuples[{ Range[Length[array1]], Range[Length[array2]]}];
Select[pairs, array1[[#[[1]]]] - 1 == array2[[#[[2]]]] &]
{{6, 2}, {8, 3}}
You can also use Outer
as follows:
Join @@ Outer[If[array1[[#]] - 1 == array2[[#2]], {##}, Nothing] &,
Range[Length[array1]], Range[Length[array2]]]
{{6, 2}, {8, 3}}
Yet other ways: variations on Roman's method using Position
and Outer
combination:
Position[1] @ Outer[Subtract, array1, array2]
Position[True] @ Outer[Equal, array1 - 1, array2]
Position[{i_, i_}]@Outer[List, array1 - 1, array2]
{{6, 2}, {8, 3}}
$endgroup$
$begingroup$
I like yourOuter[Subtract, ...
way!
$endgroup$
– Roman
2 hours ago
$begingroup$
thank you @Roman. Didn't think of usingPosition
until i saw your answer.
$endgroup$
– kglr
2 hours ago
add a comment |
$begingroup$
You can use Tuples
to construct all pairs of positions and use Select
to pick the pairs that satisfy your condition:
pairs = Tuples[{ Range[Length[array1]], Range[Length[array2]]}];
Select[pairs, array1[[#[[1]]]] - 1 == array2[[#[[2]]]] &]
{{6, 2}, {8, 3}}
You can also use Outer
as follows:
Join @@ Outer[If[array1[[#]] - 1 == array2[[#2]], {##}, Nothing] &,
Range[Length[array1]], Range[Length[array2]]]
{{6, 2}, {8, 3}}
Yet other ways: variations on Roman's method using Position
and Outer
combination:
Position[1] @ Outer[Subtract, array1, array2]
Position[True] @ Outer[Equal, array1 - 1, array2]
Position[{i_, i_}]@Outer[List, array1 - 1, array2]
{{6, 2}, {8, 3}}
$endgroup$
$begingroup$
I like yourOuter[Subtract, ...
way!
$endgroup$
– Roman
2 hours ago
$begingroup$
thank you @Roman. Didn't think of usingPosition
until i saw your answer.
$endgroup$
– kglr
2 hours ago
add a comment |
$begingroup$
You can use Tuples
to construct all pairs of positions and use Select
to pick the pairs that satisfy your condition:
pairs = Tuples[{ Range[Length[array1]], Range[Length[array2]]}];
Select[pairs, array1[[#[[1]]]] - 1 == array2[[#[[2]]]] &]
{{6, 2}, {8, 3}}
You can also use Outer
as follows:
Join @@ Outer[If[array1[[#]] - 1 == array2[[#2]], {##}, Nothing] &,
Range[Length[array1]], Range[Length[array2]]]
{{6, 2}, {8, 3}}
Yet other ways: variations on Roman's method using Position
and Outer
combination:
Position[1] @ Outer[Subtract, array1, array2]
Position[True] @ Outer[Equal, array1 - 1, array2]
Position[{i_, i_}]@Outer[List, array1 - 1, array2]
{{6, 2}, {8, 3}}
$endgroup$
You can use Tuples
to construct all pairs of positions and use Select
to pick the pairs that satisfy your condition:
pairs = Tuples[{ Range[Length[array1]], Range[Length[array2]]}];
Select[pairs, array1[[#[[1]]]] - 1 == array2[[#[[2]]]] &]
{{6, 2}, {8, 3}}
You can also use Outer
as follows:
Join @@ Outer[If[array1[[#]] - 1 == array2[[#2]], {##}, Nothing] &,
Range[Length[array1]], Range[Length[array2]]]
{{6, 2}, {8, 3}}
Yet other ways: variations on Roman's method using Position
and Outer
combination:
Position[1] @ Outer[Subtract, array1, array2]
Position[True] @ Outer[Equal, array1 - 1, array2]
Position[{i_, i_}]@Outer[List, array1 - 1, array2]
{{6, 2}, {8, 3}}
edited 2 hours ago
answered 3 hours ago
kglrkglr
186k10203422
186k10203422
$begingroup$
I like yourOuter[Subtract, ...
way!
$endgroup$
– Roman
2 hours ago
$begingroup$
thank you @Roman. Didn't think of usingPosition
until i saw your answer.
$endgroup$
– kglr
2 hours ago
add a comment |
$begingroup$
I like yourOuter[Subtract, ...
way!
$endgroup$
– Roman
2 hours ago
$begingroup$
thank you @Roman. Didn't think of usingPosition
until i saw your answer.
$endgroup$
– kglr
2 hours ago
$begingroup$
I like your
Outer[Subtract, ...
way!$endgroup$
– Roman
2 hours ago
$begingroup$
I like your
Outer[Subtract, ...
way!$endgroup$
– Roman
2 hours ago
$begingroup$
thank you @Roman. Didn't think of using
Position
until i saw your answer.$endgroup$
– kglr
2 hours ago
$begingroup$
thank you @Roman. Didn't think of using
Position
until i saw your answer.$endgroup$
– kglr
2 hours ago
add a comment |
$begingroup$
This is a general solution for any condition between the elements of array1
and array2
:
Position[Outer[List, array1, array2], {i_, j_} /; j == i - 1]
{{6, 2}, {8, 3}}
@kglr's solutions are simpler than this when the condition is a difference as in the given problem.
$endgroup$
add a comment |
$begingroup$
This is a general solution for any condition between the elements of array1
and array2
:
Position[Outer[List, array1, array2], {i_, j_} /; j == i - 1]
{{6, 2}, {8, 3}}
@kglr's solutions are simpler than this when the condition is a difference as in the given problem.
$endgroup$
add a comment |
$begingroup$
This is a general solution for any condition between the elements of array1
and array2
:
Position[Outer[List, array1, array2], {i_, j_} /; j == i - 1]
{{6, 2}, {8, 3}}
@kglr's solutions are simpler than this when the condition is a difference as in the given problem.
$endgroup$
This is a general solution for any condition between the elements of array1
and array2
:
Position[Outer[List, array1, array2], {i_, j_} /; j == i - 1]
{{6, 2}, {8, 3}}
@kglr's solutions are simpler than this when the condition is a difference as in the given problem.
edited 2 hours ago
answered 2 hours ago
RomanRoman
2,055715
2,055715
add a comment |
add a comment |
MetallicSilence is a new contributor. Be nice, and check out our Code of Conduct.
MetallicSilence is a new contributor. Be nice, and check out our Code of Conduct.
MetallicSilence is a new contributor. Be nice, and check out our Code of Conduct.
MetallicSilence is a new contributor. Be nice, and check out our Code of Conduct.
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%2f192349%2fcomparing-two-arrays-of-unequal-length%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