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

Carlos J. Gil Bellosta cgb en datanalytics.com
Mar Ene 24 14:40:56 CET 2017


gridsizes <- function(n){
  a <- 1:sqrt(n)
  a <- a[n %% a == 0]

  if(length(a) == 0)
    stop("No se ha encontrado ninguna opción")

  a <- a[length(a)]
  c(a, n / a)
}

gridsizes(50)


gridsizes.alt <- function(n, c1, c2){
  a <- 1:(n/2)
  a <- a[n %% a == 0]
  a <- a[c1 %% a == 0]

  b <- n / a
  b <- b[c2 %% b == 0]

  if(length(b) == 0)
    stop("No se ha encontrado ninguna opción")

  idx <- which.max(abs(b - n / b))

  c(n / b[idx], b[idx])
}


gridsizes.alt(100, 25, 4)


Un saludo,

Carlos J. Gil Bellosta
http://www.datanalytics.com

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

	[[alternative HTML version deleted]]



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