# List operation for beginner Usage of #1 and #2 (in stead of using for loop)

I have trouble using list operation of #1 and #2. Could you please tell me how to make it?

I prepared the list as below,

np = 10;
r = RandomReal[{-20, 20}, {np, 2}];
the = RandomReal[{0, 360}, np];
points = {r[[#]][[1]] + Cos[the[[#]] Degree],
r[[#]][[2]] + Sin[the[[#]] Degree]} & /@ Range[np];
join1 = Join[r, points] (* Joining the list of r and points *)


Then, what I want is to make 3 sets of join1, without using for loop,so

n = 3;

listR = Table[RandomReal[{-20, 20}, {np, 2}], n]
listThe = Table[RandomReal[{0, 360}, np], n]
(* I tried to make these 3 times and conbine, so I tried like followings *)
points3 = {listR[[#1]][[#2]][[1]] +
Cos[listThe[[#1]][[#2]] Degree],
listR[[#1]][[#2]][[2]] + Sin[listThe[[#1]][[#2]] Degree]} &[Range[n], Range[np]]


However, error occurs and I have no answer for it.

At last, I want to get list of joining listR and points3, so it might be like

join2 = Join[listR[[#1]][[#2]], points3[[#1]][[#2]]] & /@ [Range[n],
Range[np]];


But I have no confidence. I wonder someone would kindly help me how to do this.

You are actually much closer than you think. I will give you the short and long ways to go about it. The longer way looks more complex, but trust me when I say that once you get accustomed to it, you will not go back from it!

The problem is that you are trying to use slots (#1,#2) in a generalised way to create higher order Table, which, to my knowledge, you cannot do. You could however just use Table!!

# Using Table, the bread & butter of lists

I would only make the minor change in your code on points3

points3T=Table[{listR[[i]][[j]][[1]] + Cos[listThe[[i]][[j]] Degree],
listR[[i]][[j]][[2]] + Sin[listThe[[i]][[j]] Degree]}, {i, 1,
n}, {j, 1, np}]


This immediately solves your problem (i believe), and then it is just a matter of joining the lists as you see it fit.

I would suggest that you have a look at the following method using its own function, as shown below:

# Creating a dedicated function

## MapThread

MapThread is a very powerful tool and well suited for what you are trying to do. It is also keeping your code a bit neater and easier to read. I will use it on your simpler, first example to convince you that it can do (in principle!) what you ultimately desire.

pointsMT=MapThread[{#1[[1]] + Cos[#2 Degree], #1[[2]] + Sin[#2 Degree]} &, {r,
the}]; (* using MapThread rather than a nested 1D Table *)
pointsMT==points (* this is to convince yourself that it works *)


## Use Module/Block to create smaller functions

This is good practice in general as it once again keeps your code readable and easy to scale, modify etc.. Here is an example of me using a Block to create a function that performs the threading once, and then I apply the function to the n=3 case by using MapThread once again.

Clear[threader]
(* this is a function that takes two lists, "r" and "the",
and then spits out the polar coordinates *)
points =
MapThread[{#1[[1]] + Cos[#2 Degree], #1[[2]] +
Sin[#2 Degree]} &, {r, the}]
]



I would normally change the variable names within threader to make it more easy for the reader to know what r and the actually mean when using the function, but I kept it here the same for simplicity.

You can convince yourself that this works as expected by checking with the previous Table function:

points3T==points3MT


Joining the two parts can be done very easily using MapThread, once again!

MapThread[Join[#1, #2] &, {listR, points3T}]


The good part about the Block and Module tools is that you can make it so that your data generation (RandomReal) and combination as well as Joining, all happen within that same block. If you prefer to have a separate function to generate the data and another for the grouping and joining, it is up to you.

• Thank you So Much for teaching me, it's very kind of you, I'm really impressed!! I haven't get an answer like this before, it's so introductory and very easy to understand. I'm so grateful to you. And yes, this is reall what I wanted to do. Now I could successfully get data, which I desired. Also, owing to your explaination, I got tips of "Block", too. Thanks again!
– rani
Commented Feb 17, 2023 at 14:25
• you're welcome :) And the documentation is always your friend.
– alex
Commented Feb 17, 2023 at 14:38
• It’s so nice of you to give me warm-hearted reply! I'm so lucky to get your instruction.
– rani
Commented Feb 17, 2023 at 14:43