zoukankan      html  css  js  c++  java
  • R语言两自定义矩阵的基本运算-实例

    #sink("matrix_history.txt")
    cat("请输入矩阵的行和列数,“,”号隔开,建议行 等于列 数:")
    number<-scan(what="character",sep=",")
    number<-matrix(number,2,2,TRUE)
    number=apply(number,2,as.numeric)
    
    row<-number[1,1]
    clo<-number[1,2]
    #print(row)
    #print(clo)
    
    
    
    
    cat("请输入第一个",row,"*",clo,"矩阵的",row*clo,"个元素:")
    a<-scan(what="character",sep=",")
    b<-matrix(a,row,clo,TRUE)
    cat("
    ")
    print(b)
    print("进行类型转换成功!  字符类型矩阵转为数字类型")
    b=apply(b,2,as.numeric)
    print("类型转换后输出:")
    print(b)
    cat("
    ")
    cat("请输入第二个",row,"*",clo,"矩阵的",row*clo,"个元素:")
    c<-scan(what="character",sep=",")
    d<-matrix(c,row,clo,TRUE)
    cat("
    ")
    print(d)
    print("进行类型转换成功!  字符类型矩阵转为数字类型")
    d=apply(d,2,as.numeric)
    print("类型转换后输出:")
    print(d)
    cat("
    ")
    
    
    
    cat("现在自动进行两矩阵的加法运算:
    ",b+d)
    cat("
    ")
    cat("
    ")
    print(b+d)
    cat("
    ")
    cat("现在自动进行两矩阵的减法运算:
    ",b-d)
    cat("
    ")
    cat("
    ")
    print(b-d)
    cat("
    ")
    cat("现在自动进行两矩阵的乘法运算:
    ",b*d)
    cat("
    ")
    cat("
    ")
    print(b*d)
    cat("
    ")
    cat("现在自动进行两矩阵的除法运算:
    ",b/d)
    cat("
    ")
    cat("
    ")
    print(b/d)
    cat("
    ")
    cat("现在自动进行两矩阵的求余运算:
    ",b%%d)
    cat("
    ")
    cat("
    ")
    print(b%%d)
    cat("
    ")
    cat("现在自动进行两矩阵的%/%运算:
    ",b%/%d)
    cat("
    ")
    cat("
    ")
    print(b%/%d)
    cat("
    ")
    try(cat("现在自动进行两矩阵的%*%运算:
    ",b%*%d))
    cat("
    ")
    cat("
    ")
    try(print(b%*%d))
    cat("
    ")
  • 相关阅读:
    进程与线程
    the art of seo(chapter seven)
    the art of seo(chapter six)
    the art of seo(chapter five)
    the art of seo(chapter four)
    the art of seo(chapter three)
    the art of seo(chapter two)
    the art of seo(chapter one)
    Sentinel Cluster流程分析
    Sentinel Core流程分析
  • 原文地址:https://www.cnblogs.com/Raodi/p/11662672.html
Copyright © 2011-2022 走看看