# Can I use unique to reduce one row, but keep reference to how many cells it was reduced from?

2 ビュー (過去 30 日間)
cjg1215 2018 年 4 月 27 日
コメント済み: cjg1215 2018 年 4 月 30 日
I have an array of 2x500. the first row does time, the second row of the column represents various modes from 10 - 100. I want to reduce the second row to uniques, but I want to know how much time is spent in each unique mode.
so my table looks like:
1 1.5 2 2.5 3 3.5 4 4.5 5
10 10 10 20 20 30 40 50 50
except it goes on for over 500 elements.
So is it possible to reduce down to unique but also reduce the first row to show time spent in each mode?
Edit: the modes can also go back down so can unique(or some other function) be used to reduce to unique only for values next to each other? For instance, below, I'd like to keep the two instances of mode 20 separate, but join reduce the ones grouped together.
1 1.5 2 2.5 3 3.5 4 4.5 5
10 20 20 30 20 20 30 40 50

#### 0 件のコメント

サインイン to comment.

### 採用された回答

Venkata Siva Krishna Madala 2018 年 4 月 30 日

Hi Colin,
I understand the operation you want to perform. However, a direct function is not available to perform the required operation. I have written a script that can be used to perform the operation.(Here in_matrix is the data you have collected and result is the data you want to see)
j=1;
t=in_matrix(2,1);
result(1,1)=t;
result(1,2)=1;
for i=2:size(in_matrix,2)
t1=in_matrix(2,i);
if t==t1
result(j,2)=result(j,2)+0.5;
else
t=t1;
j=j+1;
result(j,1)=t1;
result(j,2)=0.5;
end
end
Thanks,

#### 1 件のコメント

cjg1215 2018 年 4 月 30 日
Perfect. Thank you.

サインイン to comment.

### その他の回答 (1 件)

Jos (10584) 2018 年 4 月 30 日
You are looking for some run-length encoding scheme:
A = [ 1 1.5 2 2.5 3 3.5 4 4.5 5
10 10 10 20 20 30 40 50 50 ]
[~, V] = runindex(A(2,:))
V is a matrix which holds the value, the start index and the length of each run in its three columns. To get the time T spend in each run, use a formula like this:
T = A(1, V(:,2)+V(:,3)) - A(1, V(:,2))
runindex can be found on the file exchange. It entails a fast, vectorised run length encoding/decoding algorithm: https://uk.mathworks.com/matlabcentral/fileexchange/56131-runindex-a--c-

#### 0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by