0
$\begingroup$

So I have two lists generated in the following fashion, I don't know if it's the most efficent way to get these, but that's beside the point right now.

Nlayer = 6;
n = 0;
layernumber = Table[n = n + (Mod[i, 2]), {i, 1, 2*Nlayer}]
n = 1;
interfacenumber = Table[n = n + (Mod[i, 2]), {i, 0, 2*Nlayer-1}]

Which returns:

{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6}

{1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7}

And I'm trying to get the Table function to apply the i-th value of each list to two different functions X and Y, getting a list of the same length as above. What I have right now is the following (which is incorrect, it returns a 12x12 matrix instead of a 1x12 table):

Table[X[i]*Y[j], {i,layernumber}, {j,interfacenumber}];

In other words, I want it to return the 12 values that would be computed below into a list. Later, this will be expanded well beyond 12 values and I want to just expand the initial list instead of typing this out many more times.

X[1]*Y[1]
X[1]*Y[2]
X[2]*Y[2]
X[2]*Y[3]
X[3]*Y[3]
X[3]*Y[4]
X[4]*Y[4]
X[4]*Y[5]
X[5]*Y[5]
X[5]*Y[6]
X[6]*Y[6]
X[6]*Y[7]

Thank you in advance for answers, I'm headed to bed and will respond in the morning.

$\endgroup$
4
  • 3
    $\begingroup$ try (X/@layernumber)( Y/@interfacenumber)? $\endgroup$
    – kglr
    Oct 17, 2020 at 5:43
  • $\begingroup$ If you have to use Table, you can try Table[X[layernumber[[i]]]*Y[interfacenumber[[i]]], {i, Length@layernumber} ] $\endgroup$
    – kglr
    Oct 17, 2020 at 6:42
  • $\begingroup$ ... or Table[X[i], {i,layernumber}] * Table[Y[j], {j,interfacenumber}] $\endgroup$
    – kglr
    Oct 17, 2020 at 6:53
  • $\begingroup$ That last one actually seems pretty obvious in hindsight. Thank you for the response! $\endgroup$
    – ArkaDarp
    Oct 17, 2020 at 14:59

2 Answers 2

1
$\begingroup$

Maybe this?

layernumber = Accumulate[Mod[Range[1, 2 Nlayer], 2]];
interfacenumber = 1 + Accumulate[Mod[Range[0, 2 Nlayer - 1], 2]];
MapThread[Times, {layernumber, interfacenumber}]
Inner[X[#1]*Y[#2] &, layernumber, interfacenumber, List]
$\endgroup$
1
  • $\begingroup$ MapThread[X[#1]*Y[#2] &, {layernumber, interfacenumber}] $\endgroup$
    – cvgmt
    Oct 17, 2020 at 6:42
1
$\begingroup$

Answer was provided in the comments to the question - by kglr:

Table[X[i], {i,layernumber}] * Table[Y[j], {j,interfacenumber}]

Which seems a little obvious in hindsight to just use two Table functions instead of one. Extrapolating this to the problem I'm working on (which has some unruly matricies) also required use of the Map function, again thanks to kglr.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.