Suppose I have a tensor of size [1:92, 1:13, 1:2000] and I need to transpose every of the 1:92 matrices and reshaping my tensor to [1:92, 1:2000, 1:13]. Sure I could use a loop, transposing every matrix seperately and combining them to a new tensor, but my intuition tells me there should be a more elegant way to do this. The sweep function seemed to me to be the right direction, trying for several hours now to solve it and getting out of ideas.
Asked
Active
Viewed 503 times
4
-
1I'm telling you again. Adding follow up questions is not the way asking works here. Post a new question. It's not a forum. – Julius Vainora Mar 20 '18 at 20:01
-
Are you sure here? I would have to post the first question again, since like I mentioned this only appears in combination with a transpose using aperm followed up by a reshape. – Eugen Mar 20 '18 at 20:08
-
Yes. The initial question was about transposing, now you want to drop a dimension. That's quite different. Also, `The order needs to stay columns before rows` is unclear and you need to provide example data and the expected output (see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for that). And you don't need to write the first question again because you have a solution to it, which has to be mentioned in the question. Also you can think like this: if your new, **clearly stated** question doesn't fit into a comment, it should be a new question. – Julius Vainora Mar 20 '18 at 20:19
1 Answers
4
Yes, there is a great way for that:
aperm(A, c(1, 3, 2))
where A
is your array and c(1, 3, 2)
shows how the dimensions of A
should be permuted.
For example,
A <- array(1:(2 * 3 * 4), 2:4)
dim(A)
# [1] 2 3 4
A[1, , ]
# [,1] [,2] [,3] [,4]
# [1,] 1 7 13 19
# [2,] 3 9 15 21
# [3,] 5 11 17 23
B <- aperm(A, c(1, 3, 2))
dim(B)
# [1] 2 4 3
B[1, , ]
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 7 9 11
# [3,] 13 15 17
# [4,] 19 21 23

Julius Vainora
- 47,421
- 9
- 90
- 102
-
-
I got a follow up problem on reshaping the transposed tensor. Transposing [1:92,1:13,1:2000] to [1:92,1:2000,1:13] works fine, now I need to reshape it to [1:92*2000,1:13] is there also a simple solution for this? Or do I need to take the detour here? – Eugen Mar 20 '18 at 01:52
-
Thanks again. My detour would've involved a loop, concating the two axis to each other, or some modulo statement should also work I guess. – Eugen Mar 20 '18 at 02:15
-
It seems this doesn't work also, it gives me same results like array_reshape(B,dim=c(2*4,3)), the order is wrong, also tried to use apply before aperm. aperm in combination with axis reduction seems problematic. – Eugen Mar 20 '18 at 18:34
-
I'm sry, I was to lazy to find out how to post code in comments, didn't knew the system here, I deleted it now. I will also vote on your answers once I reach 15 rep. I really didn't mentioned that the order should stay. – Eugen Mar 20 '18 at 19:39
-
@Eugen, writing short code in comments is fine, but you should press "edit" on your question when you have something substantial to add. In this case, however, you should post a new question because those are two different questions. It's true that upvoting requires 15 rep, but accepting an answer doesn't require any. – Julius Vainora Mar 20 '18 at 19:43
-
Apparentely my follow up problem only appears exactly in that constellation. Just array_reshaping and/or removing axis doesn't cause any ordering problems, but in combination with a transponse on the "submatrix" causes later ordering problems. – Eugen Mar 20 '18 at 19:52