Hello everyone,
I have the matrix A as follow:
A = [25,240,15,H;25,255,10,R;25,265,20,H;63,240,10,W;63,250,35,L];
The first column in matrix A is the ID that indicates that which row is belongs to what ID (e.g. the first,second and tird row are belongs to ID = 25 , and, forth and fifth row are belong to ID = 63.)
So, I want matrix B like to follow:
B = [ID 240 245 250 255 260 265 270 275 280
25 H H H R R H H H H
63 W W L L L L L L L];
Each DU is divided by 3 (e.g. 15/3 = 5) and then by each of these 5 is added to the ST (e.g. 240, 245 (=240+5), 250 (=245+5).

1 件のコメント

Moe
Moe 2016 年 2 月 18 日
Characters H, R, W and L can change with any numbers as well. For example, instead of H = 100, R = 101, W = 102, L = 103.

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

 採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 2 月 19 日
編集済み: Andrei Bobrov 2016 年 2 月 21 日

0 投票

n = xlsread('try1.xlsx',1,'A2:D6');
[a1,~,c1] = unique(n(:,1));
c3 = (n(:,3)-240)/5+1;
c4 = n(:,end)/5;
n2 = n(:,2);
z = accumarray(c1,c4);
b = cumsum(accumarray([c3,c1],1,[z(1),max(c1)]));
out1 = n2(bsxfun(@plus,b,cumsum([0,b(end,1:end-1)])))';
out = [nan,240+(0:z(1)-1)*5;[a1,out1]]

5 件のコメント

Moe
Moe 2016 年 2 月 19 日
Thanks Andrei, but the code is not working!
Andrei Bobrov
Andrei Bobrov 2016 年 2 月 19 日
Typo :), corrected.
Moe
Moe 2016 年 2 月 21 日
編集済み: Moe 2016 年 2 月 21 日
Thanks Andrei,
I tried your code with a larger database, but results aren't correct! I've attached an input file (try2). For example, after running code, look into ID 271, and ST 410. According to excel file the Code should be 103 until ST 1135, but in Matlab output code is 101 still.
Also, I used "ceil" function to get positive integer subscripts for c3 and c4.
Can you please have a look to the code with new data? Thanks in advance!
n = xlsread('try2.xlsx',1);
[a1,~,c1] = unique(n(:,1));
c3 = ceil((n(:,3)-240)/5+1);
c4 = ceil(n(:,end)/5);
n2 = n(:,2);
z = accumarray(c1,c4);
b = cumsum(accumarray([c3,c1],1,[z(1),max(c1)]));
out1 = n2(bsxfun(@plus,b,[0,b(end,1:end-1)]))';
out = [nan,240+(0:z(1)-1)*5;[a1,out1]]
Andrei Bobrov
Andrei Bobrov 2016 年 2 月 21 日
I corrected answer and corrected your data (see try2_1.xlsx)
Moe
Moe 2016 年 2 月 22 日
Grear Andrei, Thanks!

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

Moe
2016 年 2 月 18 日

コメント済み:

Moe
2016 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by