0

私は、以下を転置するif、elseループを作成しようとして困惑しました:

Subject: Date: Result:
AAA 02/01/12 10
AAA 02/02/12 12
AAA 02/03/12 14
BBB 02/01/12 25
BBB 02/02/12 26
BBB 02/03/12 27
CCC 01/01/12 66
CCC 01/02/12 70
CCC 01/03/12 75

以下に示すように、情報が列間で転置されることを望んでいます。

SUBJECT 01/01/12 01/02/12 01/03/12 02/01/12 02/02/12 02/03/12
AAA RESULT
BBB RESULT
CCC RESULT

列ごとに1つの日付、行ごとに1つの件名のみが存在する必要があります。結果はそれぞれの主題と一致し、適切なセルに配置されます。データは、少数の主題から数千の主題、日付、および結果まで、どこからでも構成できます。一部の結果は、非数値(NaN)で構成されている場合があります。また、件名と日付はランダムな順序で表示される場合があり、件名は数字と文字列文字で構成されている場合があります。

@ amro&superbestを更新

日付形式が800317==mar / 17/1980のテキストファイルがある場合、これをインポートして、作成したコードを変更するにはどうすればよいですか?再度、感謝します。

4

2 に答える 2

2

データセット配列オブジェクトにデータが格納されている場合は、 unstackメソッドが探しているものです(トールからワイドへの変換と呼ばれることもあります)。


データセットクラスなしでこれを行う方法を示す簡単な例を作成しました。

%# cell array: subjects, dates, values
data = {
    'AA' '2012-05-01' 0.1
    'AA' '2012-05-03' 0.2
    'BB' '2012-05-02' 0.3
    'CC' '2012-05-01' 0.4
    'CC' '2012-05-02' 0.5
    'CC' '2012-05-03' 0.6
};

[subjects,~,subjectsMap] = unique(data(:,1));
[dates,~,datesMap] = unique(data(:,2));
M = nan(numel(subjects),numel(dates));
for i=1:numel(subjects)
    %# get all rows with subject == subject_i
    rIdx = (subjectsMap == i);
    %# fill values at this row for the specified columns
    M(i,datesMap(rIdx)) = cell2mat(data(rIdx,3));
end

D = cell(size(M)+1);
D(2:end,2:end) = num2cell(M);       %# fill values
D(1,2:end) = dates;                 %# column headers
D(2:end,1) = subjects;              %# row headers

変換前(トール)と変換後(ワイド)のデータは次のとおりです。

>> data
data = 
    'AA'    '2012-05-01'    [0.1]
    'AA'    '2012-05-03'    [0.2]
    'BB'    '2012-05-02'    [0.3]
    'CC'    '2012-05-01'    [0.4]
    'CC'    '2012-05-02'    [0.5]
    'CC'    '2012-05-03'    [0.6]

>> D
D = 
      []    '2012-05-01'    '2012-05-02'    '2012-05-03'
    'AA'    [       0.1]    [       NaN]    [       0.2]
    'BB'    [       NaN]    [       0.3]    [       NaN]
    'CC'    [       0.4]    [       0.5]    [       0.6]
于 2012-05-28T07:54:03.103 に答える
1

これがあなたが望むことをするはずのスクリプトです:

% Clean up
clc
clear

% Hardcoded example input
input = {
    'AAA'    '02/01/12'    10
    'AAA'    '02/02/12'    12
    'AAA'    '02/03/12'    14
    'BBB'    '02/01/12'    25
    'BBB'    '02/02/12'    26
    'BBB'    '02/03/12'    27
    'CCC'    '01/01/12'    66
    'CCC'    '01/02/12'    70
    'CCC'    '01/03/12'    75
    };

% Figure out how many rows and columns there will be
header_row = unique(input(:, 2));
header_col = unique(input(:, 1));

% Pre-allocation for better performance
output = cell(length(header_col), length(header_row));

% Rearrange the array
for i = 1:size(input, 1)
    % Find to which date and subject this element belongs
    subject = find(strcmp(header_col, input{i, 1}));
    date = find(strcmp(header_row, input{i, 2}));

    % Put the value in the appropriate slot
    output{subject, date} = input{i, 3};
end

% Add header columns and rows and print the result
result = [['SUBJECT' header_row']; [header_col output]];
于 2012-05-28T08:42:28.813 に答える