Extract time of stimulation using bwlabel function

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 Kommentare

Guillaume
Guillaume am 10 Aug. 2018
Bearbeitet: Guillaume am 10 Aug. 2018
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.
The output of your code registers only the the position of the last value (6). I need a output matrix like this:
if true
5 4
12 9
18 15
24 21
30 28
38 38
end
That defines the position of the values 1-6 for each column, respectively.
Sorry for the amount of comments! Thanks!
Oh yes, it was only looking for a diff of 1. Looking for any positive diff fixes it. See edit.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Guillaume
Guillaume am 10 Aug. 2018
Bearbeitet: Guillaume am 10 Aug. 2018
[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 Kommentare

Hi,
Basically what I want is to determine the row of the first appearances of 1 - 3 for a column of "n" elements. For example:
0 1
1 0
0 2
2 3
0 0
3 0
firstTimes1 = 2 1 firstTimes2 = 4 3 firsTimes3 = 6 4
This in a lop for "n" elements because my column can have different sizes accordingly to the sample to be analyzed!
Thanks! Best,
Guillaume
Guillaume am 10 Aug. 2018
Bearbeitet: Guillaume am 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).

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 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 Kommentare

Thanks for the help! I tried your code and gives the following error:
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Best!
How did you change it? Because it works as it, with the matrix you provided.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2014a

Gefragt:

am 10 Aug. 2018

Kommentiert:

am 10 Aug. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by