[R] Help on a matrix task

Adrian DUSA adi at roda.ro
Thu Dec 8 20:20:11 CET 2005


On Tuesday 06 December 2005 14:41, JeeBee wrote:
> [...]
> N = 4
>
> input_numbers = seq((2^N)-1, 0, -1)
> # convert to binary matrix
> input_mat = NULL
> for(i in seq(N-1,0,-1)) {
>   new_col = input_numbers %% 2
>   input_mat = cbind(new_col, input_mat)
>   input_numbers = (input_numbers - new_col) / 2
> }
> colnames(input_mat) = NULL

A little late, but wouldn't be more simple to create input_mat with:
N <- 4
input_mat <- matrix(NA, ncol=N, nrow=2^N)
for (i in 1:N) input_mat[,i] <- c(rep(0, 2^(N - i)), rep(1, 2^(N - i)))

HTH,
Adrian

-- 
Adrian DUSA
Romanian Social Data Archive
1, Schitu Magureanu Bd
050025 Bucharest sector 5
Romania
Tel./Fax: +40 21 3126618 \
          +40 21 3120210 / int.101




More information about the R-help mailing list