Count and "synchronize" events in several columns
Afficher commentaires plus anciens
Y'all,
sorry if the question doesn't fully capture the actual problem, I simply don't know how to exactly describe it in a concise manner. This is the problem: I have a matrix consisting of columns with occurrences of 1s (the first column is simply a time counter). Each series of 1s without an NaN in between is an "event". Like this:
1 NaN NaN NaN
2 NaN NaN NaN
3 1 NaN NaN
4 1 1 NaN
5 NaN NaN NaN
6 NaN NaN NaN
7 NaN 1 NaN
8 1 1 NaN
9 1 NaN 1
10 1 NaN 1
11 NaN NaN NaN
12 1 1 NaN
13 1 1 NaN
14 NaN NaN NaN
15 NaN 1 NaN
16 NaN 1 1
A total of three events in column 2, four events in column 3, and two events in column 4 (again, column 1 is a time step/counter). I need to count the number of events and match them up with the respective events in the other columns. The end result should look like this:
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 1 NaN NaN 1
4 1 1 NaN 1
5 NaN NaN NaN NaN
6 NaN NaN NaN NaN
7 NaN 1 NaN 2
8 1 1 NaN 2
9 1 NaN 1 2
10 1 NaN 1 2
11 NaN NaN NaN NaN
12 1 1 NaN 3
13 1 1 NaN 3
14 NaN NaN NaN NaN
15 NaN 1 NaN 4
16 NaN 1 1 4
Any input appreciated! Oh, the actual matrix consists of more than 3 columns with events (for now there are 4 columns, but it will get extended to about 15 eventually).
Thanks, F
1 commentaire
Azzi Abdelmalek
le 31 Août 2016
How did you get the fifth column?
Réponse acceptée
Plus de réponses (2)
Azzi Abdelmalek
le 31 Août 2016
A=[1 NaN NaN NaN
2 NaN NaN NaN
3 1 NaN NaN
4 1 1 NaN
5 NaN NaN NaN
6 NaN NaN NaN
7 NaN 1 NaN
8 1 1 NaN
9 1 NaN 1
10 1 NaN 1
11 NaN NaN NaN
12 1 1 NaN
13 1 1 NaN
14 NaN NaN NaN
15 NaN 1 NaN
16 NaN 1 1]
idx=any(A(:,2:end)==1,2)'
ii1=strfind([0 idx],[0,1])
ii2=strfind([idx 0],[1,0])
c=nan(size(A,1),1)
for k=1:numel(ii1)
c(ii1(k):ii2(k))=k
end
out=[A c]
1 commentaire
Fabs
le 31 Août 2016
Azzi Abdelmalek
le 31 Août 2016
idx=any(A(:,2:end)==1,2)'
[~,~,ii]=unique(nonzeros(cumsum(~idx).*idx))
c=nan(size(A(:,1)))
c(logical(idx))=ii
out=[A c]
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!