English 中文(简体)
在嵌入的 IFP 语句中转换和排序
原标题:matlab transposing & sorting in embedded IF statements
  • 时间:2012-05-28 06:50:28
  •  标签:
  • matlab

我被绊倒了 试图创造一个如果, ELS环 传递以下内容:

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

每个列只应有一个日期,每行只应有一个主题。结果将匹配各自的主题,并放在适当的单元格中。数据可以包含从几个主题、日期和结果组成的数据。有些结果可能包含非数字值(NAN)。此外,主题和日期可以任意排列,主题可以由数字字符和字符串字符组成。

UPDATE @amro & superbest

If I had a text file with the date format:800317==mar/17/1980, How would I import this and modify the codes you ve written? THanks again.

最佳回答

这里的脚本应该做你想做的事:

% 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]];
问题回答

如果您将数据存储在“http://www.mathworks.com/help/toolbox/stats/datasetglass.html”中,则您正在寻找的就是“nofollow”>>datastraction 对象,然后是“a href=” http://www.mathworks.com/help/toolbox/stats/dataset.unstack.html”。 rel=“nofollow”>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]




相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签