Sebastian Kruk
2017-Jan-24 13:18 UTC
[R-es] Convertir programa Matlab a R sacado de Threshold Models of Collective Behavior de Michèle Lai & Yann Poltera
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 funcin:
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,
Sebastin.
[[alternative HTML version deleted]]
Carlos Ortega
2017-Jan-24 13:40 UTC
[R-es] Convertir programa Matlab a R sacado de Threshold Models of Collective Behavior de Michèle Lai & Yann Poltera
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]]
Carlos J. Gil Bellosta
2017-Jan-24 13:40 UTC
[R-es] Convertir programa Matlab a R sacado de Threshold Models of Collective Behavior de Michèle Lai & Yann Poltera
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]]
Sebastian Kruk
2017-Jan-24 19:30 UTC
[R-es] Convertir programa Matlab a R sacado de Threshold Models of Collective Behavior de Michèle Lai & Yann Poltera
Hola Carlos.
Uní los dos programas que me enviaste de la siguiente forma:
gridsizes <- function(n,...){
args <- list(...)
if(length(args) == 0) {
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)
} else {
a <- 1:(n/2)
a <- a[n %% a == 0]
a <- a[args[[1]] %% a == 0]
b <- n / a
b <- b[args[[2]] %% 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])
}
}
¿Cómo haría si quiero partirlo en n grillas, siendo n>2?
Saludos,
Sebastián.
De: Carlos J. Gil Bellosta
Enviado: martes, 24 de enero de 2017 10:40
Para: Sebastian Kruk
CC: r-help-es en r-project.org
Asunto: Re: [R-es] Convertir programa Matlab a R sacado de Threshold Models of
Collective Behavior de Michèle Lai & Yann Poltera
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]]