0
$\begingroup$

I am looking for an efficient way to perform the following operation without For loops

For[a = 1, a <= 2, a++,
 For[b = 1, b <= 2, b++,
  For[c = 1, c <= 2, c++,
   Subscript[
     EV, Aindex[[a]], Bindex[[b]], Cindex[[c]]] = 
     EVec[[a, b, c]];
   ]
  ]
 ]

where Aindex (similarly B,C) is a vector of indices relevant to my problem. In other words all I am doing is changing the label of the index. EVec carries all the information that I need, but using array index a,b,c >=1 is not an intuitive way of indexing in my case. Suggestions?

$\endgroup$
3
0
$\begingroup$

Can we assume the index vectors have no duplicates? If so, then using a helper function:

ClearAll[EV, Subscript]
Evec = RandomInteger[{0, 10}, {3, 3, 3}]  (* arbitrary *)
{Aindex, Bindex, Cindex} = Permutations[Range[3]][[;; 3]] - 2(* for example *)
fpos[i_, j_, k_] := Flatten@MapThread[Position, {{Aindex, Bindex, Cindex}, {i, j, k}}]
Subscript[EV, i_, j_, k_] := Extract[Evec, fpos[i, j, k]]
$\endgroup$
4
  • $\begingroup$ Hi Alan, this wouldn't work. Because i,j,k in your suggestion will have to be integers as they index an array. I want to have the subscript to be read from a different list Aindex[[i]], Bindex[[i]] etc. In other words, its like a hash-lookup table that I am creating. $\endgroup$ Jul 27 at 4:30
  • $\begingroup$ See edit for a new possibility. $\endgroup$
    – Alan
    Jul 27 at 15:13
  • $\begingroup$ It still looks like i,j,k need to be positive integers. Sorry for not being clear earlier. Let me explain the expected output. EVec[[a,b,c]] is NxNxN where a,b,c\in{1,2,...N} each. But the indices 1,2,3 etc are abstract. It helps, in my case to map {a,b,c} to a different scheme {Aindex,Bindex,Cindex} such that Aindex\in{-1,0.5,...}. Similarly something for B,C. The labels in the Aindex make it intuitive to write a larger code. In other words, for a triplet {a,b,c} there is a unique triplet (Aindex,Bindex,Cindex) such that Evec[a,b,c] = EV[Aindex,Bindex,Cindex]. That's what my 3 for loops did. $\endgroup$ Jul 27 at 19:11
  • $\begingroup$ @SaurabhMaiti Oh right, sorry. I think I understand now. (New edit.) $\endgroup$
    – Alan
    Jul 27 at 21:56

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