Hi, I have an input matrix of 2 columns data, the first column is time index data and the second one is data which represent the status of that number (which says, if the number is positive, the number label is ‘On’ whereas when the number is negative, the number label is ‘Off’). The process of ‘On’ (positive)/ ’Off’ (negative) for each number is time sequential data (data in the 2nd column) in which the frequency for ‘On’/ ‘Off’ state for each number is arbitrary. The ‘Off’ state for each number is the one which is the closest in time index to positive value of the same number.
Thank you. Bundit
I would like to make partitions of the input matrix to get positive/negative value that represent ‘On’/’Off’ state of each existing number in the matrix. The example is shown in the attached image

 採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 4 月 22 日
編集済み: Andrei Bobrov 2014 年 4 月 24 日

0 投票

[~,~,c] = unique(abs(X(:,2)));
out = accumarray(c,(1:numel(c))',[],@(x){X(x,:)});
add
[a,~,c] = unique(abs(X(:,2)));
out = [a, accumarray(c,-sign(X(:,2)).*X(:,1))];

5 件のコメント

Bundit Buddhahai
Bundit Buddhahai 2014 年 4 月 23 日
Many thanks for your comments. Additionally, my desired output is to find the sum of time interval from positive to negative value of each number. From the example: Number 2: Time interval is 5-1 = 4 Number 3: Time interval is 4-2 = 2 Number 4: Time interval is (6-3)+(8-7) = 4
How I can write MATLAB code to work this? Thanks and regards, Bundit
Andrei Bobrov
Andrei Bobrov 2014 年 4 月 24 日
please see code after add in this answer
Bundit Buddhahai
Bundit Buddhahai 2014 年 4 月 25 日
Hi Bobrov, I can't see the code you mentioned about. Can you put it again,please?
Many thanks and regards, Bundit.
Andrei Bobrov
Andrei Bobrov 2014 年 4 月 25 日
[a,~,c] = unique(abs(X(:,2)));
out = [a, accumarray(c,-sign(X(:,2)).*X(:,1))];
Bundit Buddhahai
Bundit Buddhahai 2014 年 4 月 28 日
Many thanks Bobrov, your method is concise but very excellent. Great!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by