4
$\begingroup$

I have a list like that: {{Integer,String},....}, integer ranges from 1 to 900 and String "I" to "IV".

And I need to find which pair of {x,y} each sublist is related to, so i can get a new list like that: {{real,real},{real,real},{real,real}....}.

I did the following code:

   Fcoord[coord : {_, _}] := (
   If[(Mod[coord[[1]], 30.]) != 0, 
    y = (Quotient[coord[[1]], 30] + 1)*100;
    x = Mod[coord[[1]], 30.]*100, y = (Quotient[coord[[1]], 30])*100;
    x = 3000.
    ];
   Which[
       coord[[2]] = "I", x; y, 
       coord[[2]] = "II", x=-1*x,
       coord[[2]] = "III",x = -1*x; y= -1*y, 
       coord[[2]] = "IV", y=-1*y
    ];
   coord[[1]] = x;
   coord[[2]] = y;
   );

BUT, The following error is occurring:

Fcoord[{300, "II"}]

Set::setps: {300,II} in the part assignment is not a symbol. >>

Set::setps: {300,II} in the part assignment is not a symbol. >>

Set::setps: {300,II} in the part assignment is not a symbol. >>

General::stop: Further output of Set::setps will be suppressed during this calculation. >>

The purpose is transform, eg., a list : {{242,"I"},{56, "IV"},etc...} , into

{{x1,y1},{x2,y2}, etc...}. Using this code:

 Fcoord[#]&/@list.

UPDATE 1:

For better understanding:

I have a collection of pairs of cooordinates, ranging from -3000 to 3000,scaled both x-axis and y-axis,that have to fall into grids. So, I created grids of 100 X 100, e.g.: in the first quadrant of cartesian coordinates, there are 900 grids, and I intend this code identify each grid as a pair of {x,y}. I use as the refference the upper right vertice of a square.

Let´s say that I want to know which coordinates are for the grid number 30, in the second cartesian quadrant, so I did the function Fcoord[...], to point the pairs {x,y} for this {grid number, "cartesian quadrant"}.

Update2:

Another example, I know that grid number 900, "IV" is x= 3000, and y = - 3000, so I want to create a function that you put the grid number and quadrant, and it tells wich coordinate is (remember,that I am using the upper right vertice, but in the 3rd and 4th quadrant you should considerer the grids 'upside down"

$\endgroup$
  • $\begingroup$ belisarius ,I did updates. $\endgroup$ – locometro Jul 3 '15 at 3:28
  • 1
    $\begingroup$ on first glance, you are using = (assignment) where you mean == (equality) ?. by the way it's good that you're trying to go with Mathematica's style. it may take a while, but it will pay off. $\endgroup$ – amr Jul 3 '15 at 3:46
  • $\begingroup$ Thanks amr. I tried = and ==, but the same error happened. $\endgroup$ – locometro Jul 3 '15 at 3:51
  • 1
    $\begingroup$ oh i see. it's because function parameters are "literal" pattern replacements (they aren't really variables), so when you type coord[[1]] = x inside, the system sees it as {300,II} = x when it is applied. there's ways around this, but i would recommend using Map as you are attempting to. Map isn't designed to modify the original list, it always gives you a completely new list. lots of $$ goes into making this inefficient-sounding thing efficient, so generally you shouldn't worry about performance with something as basic as Map. Also take a look at functions such as NestList. $\endgroup$ – amr Jul 3 '15 at 6:25
  • 1
    $\begingroup$ Here is a version of the code to show a few different things/tricks: Fcoord[{n_, s_}] := Module[{x, y}, {y, x} = 100*If[Divisible[n, 30], QuotientRemainder[n, 30] + {1, 0}, {Quotient[n, 30], 30}]; s /. { "I" -> {x, y}, "II" -> {-x, y}, "III" -> {-x, -y}, "IV" -> {x, -y}}]; Fcoord /@ {{242, "I"}, {56, "IV"}} Note that Mathematica has functions for a bunch of stuff, even for things you would normally think are too specialized. $\endgroup$ – amr Jul 3 '15 at 6:31
4
$\begingroup$

I haven't followed your logic carefully, but you probably want something like

Fcoord[coord : {_?NumericQ, _String}] :=
 Module[{x, y},
  If[(Mod[coord[[1]], 30]) != 0,
   y = (Quotient[coord[[1]], 30] + 1)*100; x = Mod[coord[[1]], 30]*100,
   y = (Quotient[coord[[1]], 30])*100; x = 3000.];

  Switch[coord[[2]],
   "I",   { x,  y},
   "II",  {-x,  y},
   "III", {-x, -y},
   "IV",  { x, -y}]
  ]

Fcoord /@ {{242, "I"}, {56, "IV"}}
(* {{200, 900}, {2600, -200}} *)

Edit

Just for fun, this is equivalent to:

Fcoord[coord : {_?NumericQ, _String}] :=
 Module[{x, y},
  If[(Mod[coord[[1]], 30]) != 0,
   y = (Quotient[coord[[1]], 30] + 1)*100; x = Mod[coord[[1]], 30]*100,
   y = (Quotient[coord[[1]], 30])*100; x = 3000.];

  {x, y} Tuples[{1, -1}, 2][[{1, 3, 4, 2}]][[FromDigits[coord[[2]], "Roman"]]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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