[R-es] Convertir programa Matlab a R sacado de Threshold Models of Collective Behavior de Michèle Lai & Yann Poltera

Carlos Ortega cof en qualityexcellence.es
Mar Ene 24 14:40:52 CET 2017


Hola,

Varias cosas:

   - En la función de Matlab, pasas como argumentos: N y un vector que
   tiene C1 en la primera componente y C2 en la segunda.
   - ¿En tu función de "R" no lo tines así, sólo tienes declarado que pasas
   "N" y con los "..." ¿pasas otras cosas?...
   - En tu función "R" inicializas "a = list(...)", pero no la rellenas de
   nada. Y más abajo estás asignando "C1=a[[1]]" y "C2=a[[2]]".
      - ¿Por qué creas esta variable "a" en "R" cuando en Matlab no hay
      nada equivalente?.

¿Qué argumentos pasas entonces a tu función en "R"? ¿Qué valores pasaste
cuando has obtenido ese error?.

Saludos,
Carlos.

2017-01-24 14:18 GMT+01:00 Sebastian Kruk <residuo.solow en gmail.com>:

> Estimados Usuarios-R:
>
> Estoy convirtiendo un programa en Matlab a R.
>
> El original lo saqué de:
>
> Lai, M., & Poltera, Y. (2009). Lecture with computer exercises: Modelling
> and simulating social systems with matlab. Tech. rep., Swiss Federal
> Institute of Technology (December 2009). 27.
>
> Ahora estoy convirtiendo la siguiente función:
>
> function sizes = gridsizes(N,varargin)
> % gridsizes(N) calculates the best factorization of N into two integers N1
> and
> % N2 such that : N1xN2 == N and N2-N1 -> min (optimal grid)
> % gridsizes(N,C1,C2) calculates the best factorization of N into two
> integers N1 and
> % N2 such that : N1xN2 == N and N2-N1 -> min, under the condition that N1
> % divides C1 and N2 divides C2 (optimal subgrids for an already existing
> % grid of size C1xC2   N)
> % gridsizes(N,C1,C2,1) returns [NaN;NaN] instead of an error message if the
> % fitting couldn't be found (used in the function N_from_d)
> sizes = NaN(2,1);
> s = sqrt(N);
> tol = 10ˆ(-12);
> N2= ceil(s);
> N1 = N/N2;
> if(nargin   3)
> C1 = varargin{1};
> C2 = varargin{2};
> while(rem(C1,N1) > tol || rem(C2,N2) > tol || rem(N1,round(N1)) > tol)
> N2 = N2+1;
> N1 = N/N2;
> if(N2 > N)
> if(nargin   4 && varargin{3} == 1)
> 45
> N1 = NaN;
> N2 = NaN;
> break;
> else
> error('Cannot find subgrid fitting to the given grid');
> end
> end
> end
> else
> while(rem(N1,round(N1)) > tol)
> N2 = N2+1;
> N1 = N/N2;
> end
> end
> sizes(1) = N1;
> sizes(2) = N2;
> end
>
> En R me quedó así:
>
> gridsizes <- function(N,...) {
> sizes <- matrix(NA,2,1)
> s = sqrt(N)
> tol = 10^(-12)
> N2 = ceiling(s)
> N1 = N/N2
> a = list(...)
> if(length(a)>=3) {
>         C1 = a[[1]]
>         C2 = a[[2]]
>                 while((C1%%N1) > tol || (C2%%N2) > tol || (N1 %%
> round(N1)) > tol) {
>                         N2 = N2+1
>                         N1 = N/N2
>                         if(N2 > N) {
>                                 if(length(a) >= 4 && a[[3]] == 1) {
>                                         N1 = NA
>                                         N2 = NA
>                                         }
>                                 else {
>                                         print("ERROR: no se puede hallar
> una subgrilla que entre en la grilla dada")
>                                         }
>                         }
>                 }
>         }
> else {
>         while((N1%%round(N1)) > tol) {
>                 N2 = N2+1
>                 N1 = N/N2
>         }
> }
> sizes[1] = N1
> sizes[2] = N2
> }
>
> En alguna parte se tranca y no sale del bucle pues si lo cancelo me
> aparece el error [1] "ERROR: no se puede hallar una subgrilla que entre en
> la grilla dada" repetido en forma indefinida.
>
> No me doy cuenta doy estoy errando.
>
> ¿Alguien se da cuenta?
>
> Saludos,
>
> Sebastián.
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-help-es mailing list
> R-help-es en r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es




-- 
Saludos,
Carlos Ortega
www.qualityexcellence.es

	[[alternative HTML version deleted]]



Más información sobre la lista de distribución R-help-es