I wanna get a result of i/j . Like 1/3, 1/5, 1/7, 3/5, 3, 5/3... Something like that. But none of my code works
Going through your attempts one by one:
Attempt 1
j = {1, 3, 5, 7, 9};
i = {1, 3, 5, 7, 9};
jase[i_, j_] := i/j
What you are doing here is three things:
- You set the value of
j
to{1, 3, ...}
- You set the value of
i
to{1, 3, ...}
- You define a function
jase
that takes two arguments namedi
andj
(no connection to the variables from before) that returnsi/j
. So e.g.jase[3,4]
will return3/4
To fix it, a first attempt could be to call jase[i,j]
:
jase[i, j]
(* {1, 1, 1, 1, 1} *)
As we can see, this is not what we want. The problem is that {...}/{...}
simply divides corresponding elements. To get all possible combinations, you can use Outer
(as shown by @kglr):
Outer[jase, i, j]
(* {{1, 1/3, 1/5, 1/7, 1/9}, {3, 1, 3/5, 3/7, 1/3},
{5, 5/3, 1, 5/7, 5/9}, {7, 7/3, 7/5, 1, 7/9},
{9, 3, 9/5, 9/7, 1}} *)
Attempt 2
i != j;
Table[i/j, j ∈ {1, 3, 5, 7, 9}; i ∈ {1, 3, 5, 7, 9}]
You're doing two things:
i != j
simply returns itself without doing anything. You're suppressing that output with the;
The syntax for the
Table
expression is wrong. To specify thati
andj
should be elements of the list{1, 3, ...}
, use{i, {1, 3, ...}}
:Table[i/j, {j, {1, 3, 5, 7, 9}}, {i, {1, 3, 5, 7, 9}}] (* {{1, 3, 5, 7, 9}, {1/3, 1, 5/3, 7/3, 3}, {1/5, 3/5, 1, 7/5, 9/5}, {1/7, 3/7, 5/7, 1, 9/7}, {1/9, 1/3, 5/9, 7/9, 1}} *)
Attempt 3
jase[i_, j_] :=
Module[{}, i ≠ j; Element[{i, j}, List[1, 3, 5, 7, 9]]; i/j]
This one defines a function jase
with two arguments, that does three things:
- Evaluate the expression
i ≠ j
(which does nothing on its own) and discard its result - Evaluate the expression
Element[{i, j}, List[1, 3, 5, 7, 9]]
(which does nothing on its own), and discard its result - Evaluate the expression
i/j
(which stays unevaluated) and return its result
So calling jase[1, 2]
gives for example:
jase[1, 2]
(* 1/2 *)
I don't think this one can be easily fixed
Discarding i==j
You'll notice that all the examples above return a bunch of ones, since the cases where i==j
are not discarded. There are several ways to do this, see e.g. the answers of @kglr and @ChrisK. A solution that is more explicit could be the following:
jase[i_, j_] := i/j
jase[i_, i_] := Nothing
Outer[jase, Range[1, 9, 2], Range[1, 9, 2]]
(* {{1/3, 1/5, 1/7, 1/9}, {3, 3/5, 3/7, 1/3}, {5, 5/3, 5/7, 5/9}, {7, 7/3, 7/5, 7/9}, {9, 3, 9/5, 9/7}} *)
This is essentially your first attempt with an additional case for jase
: The second definition says "if both arguments are the same, then return Nothing
". And Nothing
is automatically removed from lists.
How's this?
range = {1, 3, 5, 7, 9};
DeleteCases[Flatten[Table[i/j, {i, range}, {j, range}]], 1]
(* {1/3, 1/5, 1/7, 1/9, 3, 3/5, 3/7, 1/3, 5, 5/3, 5/7, 5/9,
7, 7/3, 7/5, 7/9, 9, 3, 9/5, 9/7} *)
range = Range[1, 9, 2];
# / DeleteCases[range, #] & /@ range // Flatten
{1/3, 1/5, 1/7, 1/9, 3, 3/5, 3/7, 1/3, 5, 5/3, 5/7, 5/9, 7, 7/3, 7/5, 7/9, 9, 3, 9/5, 9/7}
Additional alternatives:
Flatten @ MapIndexed[Drop] @ Outer[Divide, range, range]
MapIndexed[Apply[Sequence] @* Drop] @ Outer[Divide, range, range]
Tuples[foo[range, range]] /. foo[a_, a_] -> Nothing /. foo -> Divide
Join @@ Table[i/j, {i, range}, {j, DeleteCases[range, i]}]
Distribute[{range, range}, List, List, DeleteCases[{##}, 1] &, Divide]
Array[Divide, {1, 1} Length@range, {MinMax@range}, DeleteCases[Flatten[{##}], 1] &]
and a Halloween special:
☺ = range;
## & @@@ (#/(☺ /. # -> (## &[])) & /@ ☺)