2015-06-19 4 views
5

So habe ich zwei separate Matrix (Mat1 und Mat2) und ich muss durch sie gehen, um eine Überprüfung zu machen. Ich muss die Ergebnisse in einer dritten Matrix speichern.R - Schleife durch verschiedene Matrizen ohne Schleife! Hilfe, um einfach einen Code

Ich fühle, dass mein Code für den Zweck sehr lang ist.

Ich wollte etwas von Ihrem Vorschlag haben, Schleifen zu vermeiden.

So sieht meine erste Matrix wie diese (dput am Ende)

wit5.001 wit5.002 wit5.003 wit5.004 wit5.005 wit5.006 wit5.007 wit5.008 wit5.009 wit5.010 
[1,]  1  1  1  1  1  1  1   1  1  1 
[2,]  1  1  1  1  1  1  1  1  1  1 
[3,]  1  1  1  1  1  1  1  1  1  1 
[4,]  1  1  1  1  1  1  1  1  1  1 
[5,]  1  1  1  1  1  1  1  0  1  1 
[6,]  1  1  1  1  1  1  1  0  0  0 
[7,]  0  1  1  1  1  1  1  1  1  1 
[8,]  1  1  1  1  1  1  1  1  1  1 
[9,]  1  1  1  1  1  1  1  1  1  1 
[10,]  1  1  1  1  1  1  1  1  1  1 

Meine zweite Matrix, die eine ähnliche Struktur aufweist.

Hier erstelle ich meine dritte Matrix - um die Ergebnisse der Überprüfung zu speichern.

matCheck <- matrix('ok', ncol = ncol(mat1), nrow = nrow(mat1)) 

Hier ist meine Schleife - dass ich

for(j in 1:ncol(mat1)){ 
    for(i in 1:nrow(mat1)){ 

    if(mat1[i,j] == 1 & mat2[i,j] == 1) 
    {matCheck[i,j] <- 'ok'} 

    if(mat1[i,j] != 1 & mat2[i,j] == 1) 
    {matCheck[i,j] <- '!'} 

     } 
    } 

Das Ergebnis der Prüfung

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] 
[1,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[2,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[3,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[4,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[5,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[6,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "!" "!" "ok" 
[7,] "!" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[8,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[9,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[10,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 

Irgendwelche Vorschläge vermeiden möchten?

Hier Matrix 1

mat1 = structure(c(1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1), .Dim = c(10L, 
10L), .Dimnames = list(NULL, c("wit5.001", "wit5.002", "wit5.003", 
"wit5.004", "wit5.005", "wit5.006", "wit5.007", "wit5.008", "wit5.009", 
"wit5.010"))) 

Hier Matrix 2

mat2 = structure(c(1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 
1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 
0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0), .Dim = c(10L, 
10L), .Dimnames = list(NULL, c("wit5.020", "wit5.021", "wit5.022", 
"wit5.023", "wit5.024", "wit5.025", "wit5.026", "wit5.027", "wit5.028", 
"wit5.029"))) 
+0

@Frank - Sorry mein Fehler - Sie haben Recht, sie sind nicht zu verschiedenen Dimensionen (ich habe einen Fehler in meiner Untergruppe gemacht). Ich habe den Code korrigiert. – giacomo

+0

Ich werde versuchen, Ihre Vorschläge Jungs Danke – giacomo

+0

ist es so einfach! oh nein ... Nach all dem Aufwand in meinen Loops;) Danke @Frank - nenne es dann als Antwort – giacomo

Antwort

4

Für das Beispiel gegeben, kann das Ergebnis als

matCheck <- (mat1 | !mat2) 

konstruiert werden Dies ist äquivalent matCheck zur Initialisierung zu wahren und dann falsch ausfüllen wo !mat1 & mat2 (wie in den OP's Schleife). Die Klammern sind optional, erleichtern aber das Lesen (denke ich).