Purchase Solution

Reading and writing matrices in Java

Not what you're looking for?

Ask Custom Question

My assignment is to test which algorithm is more efficient in transposing a matrix: a Naive or Fast Sparse algorithm. I need to construct functions for reach algorithm that call from an input file, which is a sparse Matrix.

A text file called M5x5.mat looks like this.

5 5 11
0 3 297
1 0 230
1 4 291
2 0 390
2 2 250
2 3 286
2 4 330
3 2 333
3 4 464
4 0 184
4 3 347

5 and 5 represent the 5x5 matrix and 11 the number of elements. M10x10 has 50 elements and so on. The matrice is set up <row, col, value>, with the first column being 'row', second column being 'col' and the third column being 'value'.

My main problem is that I don't know how to convert a text file like this into a matrice that I can use. I wasn't taught how to in my previous classes and I think my professor assumes that we know how.

I need to know how to print out the matrice as it appears on the text and a new transposed Matrice.

My primary assignment was to measure each algorithm in efficiency. I do this by adding units every time the for loop executes.

This is the Fast Transpose algorithm in pseudocode, using the units counter as mentioned:

start of algorithm/

for (i=0; i < M[0].row; i++)
rowterms[i]=0;
units++;

for (i=1; i<=M[0].value; i++)
rowterms[M[i].col]++;
units++;

startingposition[0]=1
for (i=1; i< M[0].row; i++)
startingposition[i] = startingposition[i-1] + rowterms[i-1]
units++;

/*primary transpose alg*/
for (i=1; i<= M[0].value; i++)
{

j=startingposition[M[i].col]++

/*Mt is the transposed matrice*/
Mt[j].row = M[i].col
Mt[j].col = M[i].row
Mt[j].value = M[i].value
units++;

/end of algorithm

That's just one of the algorithms.

Summing up what I need:

1. How to call the input matrice files and implement them in the functions. Or to convert the txt file into a matrice?
2. How to print out the matrices.
3. I would like to have the pseudocode above converted into a java function complemented with the calling of the Matrice file, but if that's too much, I understand.

Any help at all would be appreciated.

Thank you.

Purchase this Solution

Solution Summary

This solution provides and explains a Java class for reading and writing a matrix of integers.

Solution Preview

This solution addresses the first and second issues listed in the posting. That is, it shows how to read in data from a file to create a matrix and it shows how to display a matrix. The entire Java program to do this is in the attached Matrix.java file.

In order to read the matrix data from a file we need to use a File object that represents the file. We create this object as follows:

<pre>
File file = new File(filename);
</pre>

Once we have a file object we can create a Scanner object to read all the data from the file. The ...

Purchase this Solution


Free BrainMass Quizzes
Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.