package Basic import breeze.linalg.{DenseMatrix, DenseVector, diag} /** * Created by legotime on 2016/4/12. */ object WorkSheet { def main(args: Array[String]) { val p1 = DenseVector(1,2,3)//DenseVector(1, 2, 3) //val p1 = new DenseVector(Array(1,2,3)) val p2 = DenseMatrix((1,2,3),(2,3,4)) //val p2 = new DenseMatrix(2,3,Array(1,2,3,2,3,4)) /** * 1 2 3 2 3 4 */ val p3 = DenseVector.zeros[Double](5)//DenseVector(0.0, 0.0, 0.0, 0.0, 0.0) val p4 = DenseMatrix.zeros[Double](3,4) /** * 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 */ val p5 = DenseVector.ones[Double](5)//DenseVector(1.0, 1.0, 1.0, 1.0, 1.0) val p6 = DenseMatrix.ones[Int](2,3) //1 1 1. //1 1 1 val p7 = DenseVector.fill(3){3.0}//DenseVector(3.0, 3.0, 3.0) val p8 = DenseVector.range(1,10)//DenseVector(1, 2, 3, 4, 5, 6, 7, 8, 9) val p9 = DenseVector.range(1,10,1)//DenseVector(1, 2, 3, 4, 5, 6, 7, 8, 9) val p10 = DenseVector.rangeD(1.0,10.0,1.0)//DenseVector(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0) val p11 = DenseVector.rangeF(1.0f,10.0f,1.0f)//DenseVector(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0) /** * DenseVector.range(start,stop,step)这三个参数必须是Int类型 * DenseVector.rangeD(start,stop,step)这三个参数必须是Double类型 * DenseVector.rangeF(start,stop,step)这三个参数必须是Float类型 */ val p12 =DenseMatrix.eye[Double](3) /** * 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 */ val p13 = diag(DenseVector(1,2,3)) /** * 1 0 0 0 2 0 0 0 3 */ //矩阵的转秩 val p14 = p2.t /**1 2 2 3 3 4 * */ val p15 = DenseVector.tabulate(3)(i=>2*i)//DenseVector(0, 2, 4) val p16 = DenseVector.tabulate(2 to 5)(i=>2*i)//DenseVector(4, 6, 8, 10) val p17= DenseMatrix.tabulate(3,2){case(i,j) =>i*j}//i和j是指下标 /** * 0 1 1 2 2 3 */ val p18= DenseMatrix.tabulate(3,2){case(j) => j}//i和j是指下标 /** * (0,0) (0,1) (1,0) (1,1) (2,0) (2,1) */ val p19 =DenseVector.rand(4)//DenseVector(0.008588557152858112, 0.9802336112812655, 0.7525716715624411, 0.20909600211246837) val p20 =DenseMatrix.rand(2,3) /**0.09496899690925487 0.29462378774022446 0.04252572604429128 *0.3511648583014808 0.6187636683787814 0.1703772951194924 */ val p21 = DenseMatrix.vertcat(p2,p6)//下连接:vertical(垂直) /** 1 2 3 2 3 4 1 1 1 1 1 1 * 记住:DenseMatrix.vertcat合并的两个矩阵必须是两个同样类型的才可以合并 */ val p22 = DenseMatrix.horzcat(p2,p6)//右连接:horizontal(水平) /** * 1 2 3 1 1 1 *2 3 4 1 1 1 */ val p23 = DenseMatrix.canDim(p21)//(4,3) /**DenseMatrix.canDim(p21)._1 = 4 * DenseMatrix.canDim(p21)._2 = 3 */ val p24 = DenseMatrix.canTranspose(p21)//转秩 /** * 1 2 1 1 * 2 3 1 1 * 3 4 1 1 */ //============================================================================================ //val a = DenseVector(1 to 10)//DenseVector(Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) //print(a(0)) Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val a = DenseVector(1,2,3,4) println(a(0))//1 println(a(1 to 3))//DenseVector(2, 3, 4) println(a(3 to 0 by -2 ))//DenseVector(4, 2) println(a(3 to 0 by 2 ))//DenseVector() println(a(1 to -2))//DenseVector(2, 3) println(a(1 to -1))//DenseVector(2, 3, 4) println(a(1 to 0))//DenseVector() println(a(-1))//4 val b = new DenseMatrix(2,4,Array(1,2,3,4,5,6,7,8)) //1 3 5 7 //2 4 6 8 //println(b(5))//错误,不像MATLAB可以减去第一列个数,依列数下去 println(b(1,1))//4 println(b(::,1))//DenseVector(3, 4),第二列 println(b(1,::))//Transpose(DenseVector(2, 4, 6, 8)),第二行 //=============================================对矩阵的操作================================================== println(b.reshape(4,2)) /**1 5 2 6 3 7 4 8 */ println(b.copy) println("B的转换:"+b.toDenseVector)//DenseVector(1, 2, 3, 4, 5, 6, 7, 8) val c = new DenseMatrix(3,3,Array(1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0)) println(diag(c))//DenseVector(1.0, 5.0, 9.0) println(c( 0 to 2,1 to 2):=5.0)//println(c( ::,1 to 2):=5.0) //5.0 5.0 //5.0 5.0 //5.0 5.0 println(c) //1.0 5.0 5.0 //2.0 5.0 5.0 //3.0 5.0 5.0 } }