[R] help with matrix

jim holtman jholtman at gmail.com
Mon Dec 31 05:21:14 CET 2007


Does this give you want you want?  I assume that you have to also
ignore the first column in matrixC or else you don't have a reasonable
comparison.  Also your data for matrixA and matrixB did not have any
spaces between the numbers, so I just took a guess at what they should
be.

> a.m <- scan(textConnection("     1.851 1.4 0.083 1.001
+ 0.877 1.3 0.116 1.33
+ 1.902 1.2 1.102 0.302
+ 0.864 0.126 1.11 0.252
+ 1.823 0.216 1.002 0.307"))
Read 20 items
> a.m <- matrix(a.m, ncol=4, byrow=TRUE)[, -1]  # ignore 1st column
>
> b.m <- scan(textConnection("    0.876 1.77 0.193 0.328
+ 0.891 1.009 0.238 1.004
+ 0.864 1.115 0.276 0.22
+ 0.887 1.306 0.166 0.239
+ 0.852 1.001 1.008 0.251"))
Read 20 items
> b.m <- matrix(b.m, ncol=4, byrow=TRUE)[, -1]
>
> c.m <- scan(textConnection(" 0.5  1.0   0.5  1.0
+ 1.0   1.0  1.0   0.5
+ 1.0   1.0  1.0   0.5
+ 1.0   1.0  1.0   0.5
+ 0.5   0.5  1.0   0.5"))
Read 20 items
> c.m <- matrix(c.m, ncol=4, byrow=TRUE)[, -1]
> closeAllConnections()
>
> result <- a.m * b.m
> # set to NA entries where c.m != 1
> is.na(result) <- c.m != 1
> result
         [,1]     [,2]     [,3]
[1,] 2.478000       NA 0.328328
[2,] 1.311700 0.027608       NA
[3,] 1.338000 0.304152       NA
[4,] 0.164556 0.184260       NA
[5,]       NA 1.010016       NA
>


On Dec 30, 2007 10:52 PM, Yingchen Wang <riddle_chen94 at yahoo.com> wrote:
> Hi, dear all:
>   I am a beginner. I appreciate any help or hint from you.
>   I am trying to do calculation with matrices. I have 3 matrices. One is matrixA, 2nd is matrixB, and last is matrixC.
>    Here is matrixA:
>       1.8511.40.0831.001
> 0.8771.30.1161.33
> 1.9021.21.1020.302
> 0.8640.1261.110.252
> 1.8230.2161.0020.307
>
> Next is matrixB:
>     0.8761.770.1930.328
> 0.8911.0090.2381.004
> 0.8641.1150.2760.22
> 0.8871.3060.1660.239
> 0.8521.0011.0080.251
>
> Last is matrixC:
>  0.5  1.0   0.5  1.0
> 1.0   1.0  1.0   0.5
> 1.0   1.0  1.0   0.5
> 1.0   1.0  1.0   0.5
> 0.5   0.5  1.0   0.5
>
> What I want is to match both matrixA and matrixB with matrixC. Ignore the 1st column in matrixA and matrixB. When the element im matrixC is 1.0, multilply the corresponding element in matrixA by the corresponding element in matrixB. Otherwise, ignore it. I wrote the following code with some error in the logic:
>
>
> A<-read.table('matrixA.txt', header=F)
> B<-read.table('matrixB.txt', header=F)
> C<-read.table('matrixC.txt', header=F)
> a1<-as.matrix(A[,1])
> arest<-as.matrix(A[,2:5])
> b1<-as.matrix(B[,1])
> brest<-as.matrix(B[,2:5])
> C<-as.matrix(C)
> if(C= =1){
> ra<-(arest*brest)
> }else {
> throw<-(arest*brest)
> }
> ra
>
> I got a warning message:
> Warning message:the condition has length > 1 and only the first element will be used in: if.....
>
> When I changed the if part to:
>  ifelse (C= =1,ra<-(arest*brest),throw<-(arest*brest))
>
> The result is the multiplication of each element in matrixA and matrixB.
>
> Anyone has an idea about what is wrong. Thanks
>
> Riddle
>
>
>      ____________________________________________________________________________________
> Be a better friend, newshound, and
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list