-1

こんにちは、私は matlab に問題があります。

データがスペースで区切られたテキスト ファイルがあります。以下に例を示します。

3 4
1 3 6
2 4 5 7
1 3 0
0 0 -1 -2
1 -1 0 -1
-1 0 -2 0
1 -1  2 3 1

私がやりたいことは、テキストファイルを読んで、次のような行列を生成することです:

size_mat=[3,4];
B=[1,3,6];
NB=[2,4,5,7];
b=[1,3,0];
A=[0, 0, -1, -2;
   1, -1, 0, -1;
  -1, 0, -2, 0];
z=[1,-1,2,3,1];

%//In details I can highlight these points : 
%//1st line is the size_mat matrix. This has always dimension 1X2. m=size_mat(1) & n=size_mat(2)
%//2nd line is the B matrix. Dimension is 1Xm 
%//3rd line is the NB matrix Dimension is 1Xn
%//4th line is the b matrix  Dimension is 1Xm
%//Starting from 5th line to the (last-1) line is my A matrix 
%//whose size is actually equal to mXn where m=size_mat(1) & n=size_mat(2)
%//Last line is the z matrix Dimension is 1X(n+1)

この matlab を行うにはどうすればよいですか?

前もって感謝します !!

編集内容をご確認ください。抽出するマトリックスのサイズを更新しました!

4

2 に答える 2

2

ステップバイステップでやってみましょう:

  1. まず、行を文字列として読み取ります。

    fid = fopen(filename, 'r');
    C = textscan(fid, '%s', 'Delimiter', '\n');
    fclose(fid);
    
  2. 次に、行を数値に変換します。

    C = cellfun(@str2num, C{:}, 'UniformOutput', false);
    
  3. 値を行列にグループ化するには、次のようにします。

    size_mat = C{1};
    B = C{2};
    NB = C{3};
    b = C{4};
    A = vertcat(C{5:end - 1}); %// or: A = cell2mat(C(5:end - 1));
    z = C{end};
    

お役に立てれば!

于 2013-09-09T08:32:23.707 に答える
1

入力ファイルが常にそのように構造化されていると信頼していることを考慮して、fgetl(file_descriptor) を使用してファイルを 1 行ずつ読み取る必要があります。次に、空白を区切り文字として使用して各行 (文字列) を分割できます。

最後に、行列 A については、(last-1) 行に到達するまで、その行列に行を追加するだけです。

于 2013-09-09T08:11:51.753 に答える