请教大神,scala map如何从txt文件读入矩阵,对其进行操作

word2vec使用说明 | 刻骨铭心查看: 1414|回复: 7|关注: 0
如何将MATLAB中Workspace中的多个矩阵数据依次保存到txt文本中
<h1 style="color:# 麦片财富积分
新手, 积分 6, 距离下一级还需 44 积分
我在对图像进行处理后得到多个矩阵数据,其形式如下图片,我怎样才能将其数据保存到txt文档中,请大神指教。
帖子最佳答案
关注者: 167
save data.txt G G1 H H1 -ascii
<h1 style="color:# 麦片财富积分
save data.txt G G1 H H1 -ascii
直接这样就行了啊!路径这些不写吗
帖子最佳答案
关注者: 167
直接这样就行了啊!路径这些不写吗
如果不是当前路径就自己加上
<h1 style="color:# 麦片财富积分
如果不是当前路径就自己加上
我是新手,还麻烦大神把全部过程详细写一下,谢谢!
<h1 style="color:# 麦片财富积分
直接这样就行了啊!路径这些不写吗
我还有一个问题就是我在Command Window中输入save data.txt G -ascii结果出现& &
&&1.& &9.& &1.& &1.& &1.& &2.
& &1.& &9.& &1.& &1.& &1.& &2.
& &1.& &9.& &1.& &1.& &1.& &2.
& &1.& &9.& &1.& &1.& &1.& &2.
& &1.& &9.& &1.& &1.& &1.& &2.
这种形式,而不是数字,请问这怎么修改?
帖子最佳答案
关注者: 167
我还有一个问题就是我在Command Window中输入save data.txt G -ascii结果出现& &
&&1.& &9.43 ...
这怎么不是数字?
<h1 style="color:# 麦片财富积分
这怎么不是数字?
不是,我的意思是本应该是像素值的也就是一个0到255的数字。我的问题得到解决了,但还是要谢谢你的回答啊!你这个也没错的,只是存储类型不同而已。
站长推荐 /2
Powered byscala学习笔记(27)
11.1 根据优先级规则,3 &#43; 4 -& 5和3 -& 4 &#43; 5是如何被求&#20540;的?
在REPL中执行即可得到结果。都是从左至右执行
12.2 BigInt类有一个pow方法,但没有用操作符字符。Scala类库的设计者为什么没有选用**(像Fortran那样)或者^(像Pascal那样)作为乘方操作符呢?
Scala中的操作符就是方法,其优先级是根据首字母来判断的,优先级如下
最高优先级:除以下字符外的操作符字符
最低优先级:赋&#20540;操作符
一般乘方的操作符是优于乘法操作的,如果使用**作为乘方的话,那么其优先级则与*相同,而如果使用^的话,则优先级低于*操作。优先级都是有问题的。故没有使用这两种操作符
11.3 实现Fraction类,支持&#43;*/操作。支持约分,例如将15/-6变为-5/2。除以最大公约数,像这样:
class Fraction(n:Int,d:Int){
& & private val num:Int = if(d==0) 1 else n * sign(d)/gcd(n,d);
& & private val den:Int = if(d==0) 0 else d * sign(d)/gcd(n,d);
& & override def toString = num &#43; &/& &#43; den
& & def sign(a:Int) = if(a & 0) 1 else if (a & 0) -1 else 0
& & def gcd(a:Int,b:Int):Int = if(b==0) abs(a) else gcd(b,a%b)
import scala.math.abs
class Fraction(n: Int, d: Int) {
private val num: Int = if (d == 0) 1 else n * sign(d) / gcd(n, d);
private val den: Int = if (d == 0) 0 else d * sign(d) / gcd(n, d);
override def toString = num + &/& + den
def sign(a: Int) = if (a & 0) 1 else if (a & 0) -1 else 0
def gcd(a: Int, b: Int): Int = if (b == 0) abs(a) else gcd(b, a % b)
def +(other:Fraction):Fraction={
newFrac((this.num * other.den) + (other.num * this.den),this.den * other.den)
def -(other:Fraction):Fraction={
newFrac((this.num * other.den) - (other.num * this.den),this.den * other.den)
def *(other:Fraction):Fraction={
newFrac(this.num * other.num,this.den * other.den)
def /(other:Fraction):Fraction={
newFrac(this.num * other.den,this.den * other.num)
private def newFrac(a:Int,b:Int):Fraction={
val x:Int = if (b == 0) 1 else a * sign(b) / gcd(a, b);
val y:Int = if (b == 0) 0 else b * sign(b) / gcd(a, b);
new Fraction(x,y)
object Test extends App{
val f = new Fraction(15,-6)
val p = new Fraction(20,60)
println(f)
println(p)
println(f + p)
println(f - p)
println(f * p)
println(f / p)
11.4 实现一个Money类,加入美元和美分字段。提供&#43;,-操作符以及比较操作符==和&。举例来说,Money(1,75)&#43;Money(0,50)==Money(2,25)应为true。你应该同时提供*和/操作符吗?为什么?
class Money(val dollar:BigInt,val cent:BigInt){
def +(other:Money):Money={
val (a,b) = (this.cent + other.cent) /% 100
new Money(this.dollar + other.dollar + a,b)
def -(other:Money):Money={
val (d,c) = (this.toCent() - other.toCent()) /% 100
new Money(d,c)
private def toCent()={
this.dollar * 100 + this.cent
def ==(other:Money):Boolean = this.dollar == other.dollar && this.cent == other.cent
def &(other:Money):Boolean = this.dollar & other.dollar || (this.dollar == other.dollar && this.cent & other.cent)
override def toString = &dollar = & + dollar + & cent = & + cent
object Money{
def apply(dollar:Int,cent:Int):Money={
new Money(dollar,cent)
def main(args:Array[String]){
val m1 = Money(1,200)
val m2 = Money(2,2)
println(m1 + m2)
println(m1 - m2)
println(m1 == m2)
println(m1 & m2)
println(Money(1,75)+Money(0,50))
println(Money(1,75)+Money(0,50)==Money(2,25))
不需要提供*和/操作。对于金额来说没有乘除操作
11.5 提供操作符用于构造HTML表&#26684;。例如:Table() | &Java& | &Scala& || &Gosling& | &Odersky& || &JVM& | &JVM,.NET&应产出:&table&&tr&&td&Java&/td&&/tr&&td&Scala&/td&&/tr&&tr&&td&Gosling…
class Table{
var s:String = &&
def |(str:String):Table={
val t = Table()
t.s = this.s + &&td&& + str + &&/td&&
def ||(str:String):Table={
val t = Table()
t.s = this.s + &&/tr&&tr&&td&& + str + &&/td&&
override def toString():String={
&&table&&tr&& + this.s + &&/tr&&/table&&
object Table{
def apply():Table={
new Table()
def main(args: Array[String]) {
println(Table() | &Java& | &Scala& || &Gosling& | &Odersky& || &JVM& | &JVM,.NET&)
11.6 提供一个ASCIIArt类,其对象包含类&#20284;这样的图形:
提供将两个ASCIIArt图形横向或纵向结合的操作符。选用适当优先级的操作符命名。纵向结合的实例
&/\_/\ & & -----
( ' ' ) &/ Hello \
( &- &) & &Scala |
&| | | & \ Coder /
(__|__) & &-----
import collection.mutable.ArrayBuffer
class ASCIIArt(str:String){
val arr:ArrayBuffer[ArrayBuffer[String]] = new ArrayBuffer[ArrayBuffer[String]]()
if (str != null && !str.trim.eq(&&)){
str.split(&[\r\n]+&).foreach{
val s = new ArrayBuffer[String]()
def this(){
def +(other:ASCIIArt):ASCIIArt={
val art = new ASCIIArt()
val length = if (this.arr.length &= other.arr.length) this.arr.length else other.arr.length
for(i &- 0 until length){
val s = new ArrayBuffer[String]()
val thisArr:ArrayBuffer[String] = if (i & this.arr.length) this.arr(i) else new ArrayBuffer[String]()
val otherArr:ArrayBuffer[String] = if (i & other.arr.length) other.arr(i) else new ArrayBuffer[String]()
thisArr.foreach(s += _)
otherArr.foreach(s += _)
art.arr += s
def *(other:ASCIIArt):ASCIIArt={
val art = new ASCIIArt()
this.arr.foreach(art.arr += _)
other.arr.foreach(art.arr += _)
override def toString()={
var ss:String = &&
arr.foreach{
ss += _.mkString(& &) + &\n&
object Test extends App{
val a = new ASCIIArt(&&& /\_/\
|( &#39; &#39; )
|&&&.stripMargin)
val b = new ASCIIArt( &&&
|&&&.stripMargin)
println(a + b * b)
println((a + b) * b)
println(a * b)
11.7 实现一个BigSequence类,将64个bit的序列打包在一个Long&#20540;中。提供apply和update操作来获取和设置某个具体的bit
class BigSequence{
var num = new Array[Int](64)
for (i &- 0 until num.length){
num(i) = -1
def pack():Long={
num.filter(_ &= 0).mkString.toLong
object BigSequence{
def apply(num:Int):BigSequence={
val b = new BigSequence
num.toString.foreach{
b.num(i) = n.getNumericValue
def main(args: Array[String]) {
val b = BigSequence(10100)
println(b.pack())
11.8 提供一个Matrix类—你可以选择需要的是一个2*2的矩阵,任意大小的正方形矩阵,或m*n的矩阵。支持&#43;和*操作。*操作应同样适用于单&#20540;,例如mat*2。单个元素可以通过mat(row,col)得到
class Matrix(val x:Int,val y:Int){
def +(other:Matrix):Matrix={
Matrix(this.x + other.x,this.y + other.y)
def +(other:Int):Matrix={
Matrix(this.x + other,this.y + other)
def *(other:Matrix):Matrix={
Matrix(this.x * other.x,this.y * other.y)
def *(other:Int):Matrix={
Matrix(this.x * other,this.y * other)
override def toString()={
var str = &&
for(i &- 1 to x){
for(j &- 1 to y){
str += &*&
str += &\n&
object Matrix{
def apply(x:Int,y:Int):Matrix= new Matrix(x,y)
def main(args: Array[String]) {
val m = Matrix(2,2)
val n = Matrix(3,4)
println(m)
println(n)
println(m + n)
println(m * n)
println(m + 2)
println(n * 2)
11.9 为RichFile类定义unapply操作,提取文件路径,名称和扩展名。举例来说,文件/home/cay/readme.txt的路径为/home/cay,名称为readme,扩展名为txt
class RichFile(val path:String){}
object RichFile{
def apply(path:String):RichFile={
new RichFile(path)
def unapply(richFile:RichFile) = {
if(richFile.path == null){
val reg = &([/\\w+]+)/(\\w+)\\.(\\w+)&.r
val reg(r1,r2,r3) = richFile.path
Some((r1,r2,r3))
def main(args: Array[String]) {
val richFile = RichFile(&/home/cay/readme.txt&)
val RichFile(r1,r2,r3) = richFile
println(r1)
println(r2)
println(r3)
11.10 为RichFile类定义一个unapplySeq,提取所有路径段。举例来说,对于/home/cay/readme.txt,你应该产出三个路径段的序列:home,cay和readme.txt
class RichFile(val path:String){}
object RichFile{
def apply(path:String):RichFile={
new RichFile(path)
def unapplySeq(richFile:RichFile):Option[Seq[String]]={
if(richFile.path == null){
Some(richFile.path.split(&/&))
def main(args: Array[String]) {
val richFile = RichFile(&/home/cay/readme.txt&)
val RichFile(r @ _*) = richFile
println(r)
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:15851次
排名:千里之外
原创:53篇
(3)(32)(10)(11)

我要回帖

更多关于 scala map 的文章

 

随机推荐