I have written following piece of codes which is a part of very larger program.
x = ConstantArray[0, 7];
i0 = Range[-2, 2]/2;
i1 = RandomReal[{-2, 2}, {400, 7}];
For[u = 1, u <= 7, u++, x[[u]] = Position[Abs[i1[[300, u]] - i0], Min[Abs[i1[[300, u]] - i0]]]];
The question is when I am going to change it in a way to look simpler by defining a function as:
index[u_] := Abs[i1[[300, u]] - i0];
and the For
loop like:
For[u = 1, u <= 7, u++, x[[u]] = Position[index[u], Min[index[u]]]];
I face this error:
SetDelayed::write: Tag Abs in Abs[{1.,0.5,0.,-0.5,-1.}+{0.498994,2.91363,1.70631,-0.708323,-1.91564,-0.708323,1.70631}][u_] is Protected.
Note that the number "300" has been chosen arbitrarily and it can be any number between "1" and "400" or the For
command itself can be in an outer loop iterating "1-400".
Thanks in advance.
ClearAll[x,i0,i1]
before trying again. $\endgroup$For
loop toPosition[index[#], Min[index[#]]] & /@ Range[7]
$\endgroup$