Extract time of stimulation using bwlabel function
Show older comments
Hi,
Consider the following matrix:
if true
0 0
0 0
0 0
0 1
1 1
1 1
1 0
1 0
0 2
0 2
0 2
2 0
2 0
2 0
0 3
0 3
0 3
3 0
3 0
3 0
0 4
0 4
0 4
4 0
4 0
4 0
0 0
0 5
0 5
5 5
5 0
5 0
0 0
0 0
0 0
0 0
0 0
6 6
6 6
6 6
6 6
end
I would like to have a code that would allow me to determine the rows for the first occurrence of 1,2,3,4,5 and 6 for the first and the second column. As it follows:
if true
5 4
12 9
18 15
24 21
30 28
38 38
end
Which are respectively the values of the rows from the values 1-6 for each individual column.
To make the code more versatile, it would be perfect to have the code in a lop for "n" columns because the input matrix (cutoff) can have different columns accordingly to the analysis performed.
Thank you in advance.
Best regards,
PS: thank you Guillaume for the help editing the post!
3 Comments
Can it be assumed that all the 2s, 3s, etc. are together in one run, as in your example matrix? Can it also be assumed that the runs are in order, 2s before 3s, before 4s, etc, as in your example matrix?
If so, then the problem reduces to finding the start of each continuous run in each column, which my answer already does.
Tiago Campelo
on 10 Aug 2018
Guillaume
on 10 Aug 2018
Oh yes, it was only looking for a diff of 1. Looking for any positive diff fixes it. See edit.
Answers (2)
[startrow, whichcolumn] = find(diff([zeros(1, size(m, 2)); m]) > 0)
If it's absolutely guaranteed that there will be the same number of runs in each column, you can transform the above into your output matrix:
output = reshape(startrow, [], size(m, 2))
2 Comments
Tiago Campelo
on 10 Aug 2018
This is completely different from what you originally asked. Rather than giving us half baked example (in this latest example each value appears only once in each column, is that really the case?), can you attach a sample of real data and the desired output.
Also, please use the {}Code button to format your posts (as I did for you for your question).
Image Analyst
on 10 Aug 2018
Is this what you want:
m = [...
0 0
1 0
1 0
0 1
0 1
2 0
2 0]
% Analyze column 1
props1 = regionprops(m(:, 1), 'PixelIdxList');
allIndexes = [props1.PixelIdxList];
firstTimes1 = allIndexes(1,:) % Get line (row) numbers of the starting elements.
% Analyze column 2
props2 = regionprops(m(:, 2), 'PixelIdxList');
allIndexes = [props2.PixelIdxList];
firstTimes2 = allIndexes(1,:) % Get line (row) numbers of the starting elements.
It basically gives the first element of each labeled region for each column, one column at a time.
m =
0 0
1 0
1 0
0 1
0 1
2 0
2 0
firstTimes1 =
2 6
firstTimes2 =
4
2 Comments
Tiago Campelo
on 10 Aug 2018
Image Analyst
on 10 Aug 2018
How did you change it? Because it works as it, with the matrix you provided.
Categories
Find more on Electrophysiology in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!