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 件のコメント

Guillaume
Guillaume 2018 年 8 月 10 日
編集済み: Guillaume 2018 年 8 月 10 日
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
Tiago Campelo 2018 年 8 月 10 日
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!
Guillaume
Guillaume 2018 年 8 月 10 日
Oh yes, it was only looking for a diff of 1. Looking for any positive diff fixes it. See edit.

サインインしてコメントする。

回答 (2 件)

Guillaume
Guillaume 2018 年 8 月 10 日
編集済み: Guillaume 2018 年 8 月 10 日

0 投票

[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 件のコメント

Tiago Campelo
Tiago Campelo 2018 年 8 月 10 日
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 2018 年 8 月 10 日
編集済み: Guillaume 2018 年 8 月 10 日
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
Image Analyst 2018 年 8 月 10 日

0 投票

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 件のコメント

Tiago Campelo
Tiago Campelo 2018 年 8 月 10 日
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!
Image Analyst
Image Analyst 2018 年 8 月 10 日
How did you change it? Because it works as it, with the matrix you provided.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeElectrophysiology についてさらに検索

製品

リリース

R2014a

質問済み:

2018 年 8 月 10 日

コメント済み:

2018 年 8 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by