Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read matrix from file in octave?

Tags:

octave

I am trying to read a matrix from file in Octave but I just can't find a solution. The input file is:

4
1 4 2 3
1 4 2 1
4 2 1 4
1 2 1 3

where 4 is the number of rows and columns. I want to be able to store that information in a matrix and to be able to use it's elements by calling them like a(2,3).

like image 978
Katelyn Avatar asked Apr 14 '16 19:04

Katelyn


People also ask

How do you import a matrix into Octave?

So you can simply use the one-liner: data = dlmread('input_file', ' ', 1, 0);

How do I read an Octave data file?

data = load('-ascii','autocleaned. txt'); Loaded the data as wanted in to a matrix in Octave. Since all the data is in fixed width columns (except the last strings), you should be able to read it line by line, using fscanf to decode the line.

How do you read a matrix from a text file in Matlab?

A = readmatrix( filename ) creates an array by reading column-oriented data from a file. The readmatrix function performs automatic detection of import parameters for your file. readmatrix determines the file format from the file extension: .

How do you read a matrix?

The dimensions of a matrix are the number of rows by the number of columns. If a matrix has a rows and b columns, it is an a×b matrix. For example, the first matrix shown below is a 2×2 matrix; the second one is a 1×4 matrix; and the third one is a 3×3 matrix.


Video Answer


1 Answers

You can use the function dlmread():

data = dlmread(file, sep, r0, c0)

Read the matrix data from a text file which uses the delimiter sep between data values.

If sep is not defined the separator between fields is determined from the file itself.

Given two scalar arguments r0 and c0, these define the starting row and column of the data to be read. These values are indexed from zero, such that the first row corresponds to an index of zero.

So you can simply use the one-liner:

data = dlmread('input_file', ' ', 1, 0);

By calling the function with r0 set to 1, you're effectively skipping the first line, which contains the (now useless) number of rows.

like image 197
jadhachem Avatar answered Sep 17 '22 21:09

jadhachem