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

2 ビュー (過去 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

採用された回答

Venkata Siva Krishna Madala
Venkata Siva Krishna Madala 2018 年 4 月 30 日
編集済み: 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,
Krishna Madala

その他の回答 (1 件)

Jos (10584)
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-

カテゴリ

Help Center および File ExchangeTime Series についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by