0

I've one file (file.csv) filled with integers in the following format:

1,2,3,4,5,6,7,8,9,100
2,5,1,3,4,6,7,8,10,200

I know that using this code:

load('FILE.csv')
x = FILE

x will be a NxM matrix,where..

  • N is the number of rows in the text file (in the example above: N=2)
  • M is the number of numbers (divided by comma) in each row (in the example above: M=10)

My goal is something similar.

I want to get two matrices:

The First matrix (y): This is the same matrix as "x" but without the last column (In the example above this is the same of loading in the matrix the following file:

   1,2,3,4,5,6,7,8,9
   2,5,1,3,4,6,7,8,10

so the result matrix is a N*(M-1) matrix (N=2 , M=10) ==> 2 rows , 9 columns

   |1 2 3 4 5 6 7 8 9 |
   |2 5 1 3 4 6 7 8 10|

The Second matrix: This is the remaining column (or more simply: a N*1 matrix and we have a row for each element of last column)

In the example above the matrix would be:

|100|
|200|

What is the easiest way to do this? (I'm a MATLAB beginner).

Thank you in advance for any hint!

4

1 に答える 1

0

FILE最も簡単な方法は、ロード後に変数を手動で分割することです

y=FILE(:,1:end-1);
x=FILE(:,end);

データ ファイルが大きく、メモリ不足が心配な場合は、次を使用してファイルをインポートできます。csvread

 y=csvread('file.csv',0,0,[0 0 n-1 m-2] # reads the first m-1 columns and n rows
 x=csvread('file.csv',0,m-1) %# reads the mth column

ただし、最初に行と列の数を知る必要があります。

于 2012-06-05T17:14:13.753 に答える