简体   繁体   中英

Matrix from matlab code into R code

The code in matlab is created to make a probability of ecosystem functioning out of loss of species in an ecosystem. Now, this code have to be translated into R. But I have problem to translate a matrix manipulation made in matlab.

In Matlab, this is the code that I have tried to translate into R code:

for j=1:N+1
multi_matrix4(:,j)=matrix(:,1);
end

In R, I have put this code within the for-loop:

+ multi.matrix4 <- matrix[,1,drop=FALSE]
+ multi.matrix4 <- multi.matrix4[,j,drop=FALSE]
+ class(multi.matrix4)

This is the message from R, that comes beneath the for-loop:

Error: subscript out of bounds

My question is: How to use R for this kind of manipulation of matrices??????

The matlab-code without the last graphs is:

clear all

% No of permutations
sim=1000;

% Total No of ecosystem functions    
N=3;

%Total dimensions
J=3;

% Total No of species in pool
total_species=4;

% No of species drawn from pool
species=4;
multi_matrix=zeros(total_species,N);

% "Threshold"
t=.5;

result=zeros(sim,J);

for i=1:sim

% %Uniformly increasing trait values
for j=1:N
matrix=rand(total_species,2);
matrix(:,1)=linspace(0,1,total_species);
matrix=sortrows(matrix,2);
multi_matrix4(:,j)=matrix(:,1);
end

%Complete covariance
matrix=rand(total_species,2);
matrix(:,1)=linspace(0,1,total_species);
matrix=sortrows(matrix,2);
for j=1:N+1
multi_matrix4(:,j)=matrix(:,1);
end

% Excess of high trait values
for j=1:N
matrix=rand(total_species,2);
X=1:total_species;X=X';
matrix(:,1)=1-exp(-0.02*X.^2);
matrix=sortrows(matrix,2);
multi_matrix4(:,j)=matrix(:,1);
end


% Deficiency of high trait values
for j=1:N
matrix=rand(total_species,2);
X=1:total_species;X=X';
% matrix(:,1)=exp((X./22.6).^3)-1;
matrix(:,1)=exp((X./13.55).^3)-1;
matrix=sortrows(matrix,2);
multi_matrix4(:,j)=matrix(:,1);
end


% Reading empirical data
warning off
% [NUMERIC,txt]=xlsread('Plant_6.xls','Sheet1');
Exp07_2 = [ 0 0.72 0.70 ; 1 1 0 ; 0.62 0 1 ; 0.36 0.69 0.61]
multi_matrix(1:total_species,1:N)=Exp07_2;
random=rand(1,N);
multi_matrix(total_species+1,1:N)=random;
multi_matrix2=sortrows(multi_matrix',total_species+1);
multi_matrix3=multi_matrix2';
multi_matrix4=multi_matrix3(1:total_species,:);
warning on


    % adding a sorting column
    random2=rand(total_species,1);
    multi_matrix4(:,N+1)=random2;
    sort_multi_matrix=sortrows(multi_matrix4,N+1);

    % loop adding one function at a time
    for j=1:J

        loss_matrix=sort_multi_matrix(1:species,1:j);
        max_value=loss_matrix>=t;
        B=any(max_value',2);
        C=all(B);
        result(i,j)=sum(C);

    end

end

% reporting
res=mean(result);
res'

The R-code looks like this:

rm()

#No of permutation
sims <- 1000;

#Total number of ecosystem functions
N <- 3

#Total dimensions
J <- 3

#Total number of species in pool
total.species <- 4

#No of species drawn from pool
species <- 4

multi.matrix <- matrix(0, nrow=total.species, ncol=N)
class(multi.matrix)

# $Threshold$
t <- .5;

# The results are to be put in a matrix
result <- matrix(0, nrow=sims, ncol=J)

for (i in 1 : sims)
{

#Uniformly increasing trait values
for (j in 1 : N)
{
matrix <- matrix(runif(total.species*2),total.species)
class(matrix)
matrix[,1] <- seq(0,1, len=total.species) # test 2
class(matrix)
matrix <- matrix[order(matrix( ,2)),]
class(matrix)
# multi.matrix4[,j,drop=FALSE] = matrix[,1,drop=FALSE]
multi.matrix4 <- matrix[,1,drop=FALSE]
multi.matrix4 <- multi.matrix4[,j,drop=FALSE]
class(multi.matrix4)
}

# Complete covariance
matrix <- matrix(runif(total.species*2),total.species)
class(matrix)
matrix[,1] <- seq(0, 1, len=total.species)
class(matrix)
matrix <- matrix[order(matrix( ,2)),]
class(matrix)
for (j in 1 : N + 1)
{multi.matrix4 <- matrix[,1,drop=FALSE]
multi.matrix4 <- multi.matrix4[,j,drop=FALSE]
class(multi.matrix4)
}

# Excess of high trait values
for (j in 1 : N)
{matrix <- matrix(runif(total.species*2),total.species)
class(matrix)
X <- 1 : total.species
X <- t(X)
matrix[,1] <- c(1 - exp(-0.02 %*% X^2)) # Hie... p. 8
matrix <- matrix[order(matrix( ,2)),]
# multi.matrix4[,j,drop=FALSE] <- matrix[,1,drop=FALSE]
# multi.matrix4[,j,drop=FALSE] <- matrix[,1]
multi.matrix4 <- matrix[,1,drop=FALSE]
multi.matrix4 <- multi.matrix4[,j,drop=FALSE]
class(multi.matrix4)
}

# Deficiency of high trait values
for (j in 1 : N)
{matrix <- matrix(runif(total.species*2),total.species)
    class(matrix)
X <- 1 : total.species
X <- t(X)
# matrix[1:4,1] <- c(exp((X/22.6)^3)-1)
matrix[1:4,1] <- c(exp((X/13.55)^3)-1)
class(matrix)
matrix <- matrix[order(matrix( ,2))]
class(matrix)
# multi.matrix4[,j,drop=FALSE] <- matrix[,1,drop=FALSE]
# multi.matrix4[,j,drop=FALSE] <- matrix[,1]
# multi.matrix4[,j] <- matrix[,1,drop=FALSE]
# class(multi.matrix4)
multi.matrix4 <- matrix[,1,drop=FALSE]
multi.matrix4 <- multi.matrix4[,j,drop=FALSE]
class(multi.matrix4)
}

# Reading empirical data
Exp_07_2 <- file(description = "Exp_07_2", open = "r", blocking = TRUE, encoding = getOption("encoding"), raw = FALSE)
Exp_07_2 <- matrix(scan(Exp_07_2),nrow=4,byrow=TRUE)
read.matrix <- function(Exp_07_2){
    as.matrix(read.table(Exp_07_2))
}
Exp_07_2
class(Exp_07_2)
multi.matrix <- matrix(c(Exp_07_2),ncol=3)
class(multi.matrix)
multi.matrix <- multi.matrix(1:total.species,1:N)  
class(multi.matrix)
random <- runif(N)
multi.matrix2 <- t(multi.matrix)[order(t(multi.matrix)[,1], t(multi.matrix)[,2], t(multi.matrix)[,3], t(multi.matrix)[,4]),]
class(multi.matrix2) 
multi.matrix3 <- t(multi.matrix2)
class(multi.matrix3)
multi.matrix4 <- multi.matrix3[1:total.species,,drop=FALSE]
class(multi.matrix4)


# Adding a sorting column
random2 <- runif(total.species,1)
random2 <- multi.matrix4[,N+1,drop=FALSE]
sort.multi.matrix <- multi.matrix4(order(multi.matrix4[,1], multi.matrix4[,2], multi.matrix4[,3],multi.matrix4[,4]),N+1,drop=FALSE)

# loop adding one function at a time
for (j in 1 : J)

{loss.matrix <- sort.multi.matrix[nrow=species,ncol=j,drop=FALSE]
    class(loss.matrix)
max.value <- loss.matrix >= t
c(B) <- any(t(max.value),2)
c(C) <- all(c(B))
result(i,j) <- c(sum(C))
}
}

# Reporting
res <- mean(result)
res
t(res)

Though I don't have Matlab and R at hand i suspect this is what is causing the problem:

In R you try to assign to a location in the matrix that does not exist, result: it fails

In Matlab you tried to assign to a location in the matrix that did not exist, result: it forgives your strange choice, expands your matrix and succeeds.

Assuming this is the problem, the solution is simple:

When creating the matrix in R, make sure that it is big enough to contain all the things you want to add to it later.

This is called initalization, and is in most cases best practice. Even in Matlab it is generally recommended to initialize your variables properly in advance where possible rather then let them grow as you go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM