MATLAB Answers

JamJan
0

How to mark periods in my matrices?

Asked by JamJan
on 23 Jul 2018
Latest activity Edited by jonas
on 24 Jul 2018
Hello,
Since I am new in Data Analysis, I need some help marking periods of consecutieve 1's.
for i=1:length(PD)
for isessions=1:PD(i).TotSessionsVid
[PD(i).FakeMatrix(isessions).Random] = randi([0 1], 10000,4);
for iconfig=1:length(PD(i).FakeMatrix(isessions).Random)
if PD(i).FakeMatrix(isessions).Random(iconfig, :) == [1 1 1 1]
PD(i).FakeMatrix(isessions).Random2(iconfig, :) = [PD(i).FakeMatrix(isessions).Random(iconfig, :), 1];
elseif PD(i).FakeMatrix(isessions).Random(iconfig, :) == [1 1 1 0]
PD(i).FakeMatrix(isessions).Random2(iconfig, :) = [PD(i).FakeMatrix(isessions).Random(iconfig, :), 1];
elseif PD(i).FakeMatrix(isessions).Random(iconfig, :) == [1 1 0 0]
PD(i).FakeMatrix(isessions).Random2(iconfig, :) = [PD(i).FakeMatrix(isessions).Random(iconfig, :), 1];
elseif PD(i).FakeMatrix(isessions).Random(iconfig, :) == [1 0 1 0]
PD(i).FakeMatrix(isessions).Random2(iconfig, :) = [PD(i).FakeMatrix(isessions).Random(iconfig, :), 1];
else
PD(i).FakeMatrix(isessions).Random2(iconfig, :) = [PD(i).FakeMatrix(isessions).Random(iconfig, :), 0];
end
end
end
end
This is my code and my goal is to find for example 5 (or as many as I want) consecutieve 1's in the 5th column of the Random2 FakeMatrices. How can I do that? I have tried && statements combined with if statements, but I believe this can be more efficient. Can anyone help me? I want to mark them as 'on'.

  4 Comments

Show 1 older comment
JamJan
on 23 Jul 2018
I'm sorry, it is quite a big data set with lots of participants and lots of sessions. :) It is as simple as this: for example I have a matrix:
1 1 1 1 1
1 1 0 0 1
1 0 1 0 1
0 1 1 0 0
0 0 1 0 0
1 0 1 0 1
0 0 1 1 0
1 0 0 0 0
1 1 0 0 1
1 0 1 0 1
1 1 0 0 1
1 1 0 0 1
0 0 0 1 0
0 0 1 0 0
0 0 1 0 0
0 0 1 1 0
And I want to mark the periods with consecutive 1's in column 5 (for example 4 in a row) as 'on' and the rest as 'off' in a new column behind this matrix. Is this more clear? I hope so!
jonas
on 23 Jul 2018
A bit, can you just also specify the exact output you want for col 5?
JamJan
on 23 Jul 2018
for example: This could be the output of the Matrix:
col 1 col2 col3 col4 col5 col6
1 1 1 1 1 on
1 1 0 0 1 on
1 0 1 0 1 on
0 1 1 0 0 off
0 0 1 0 0 off
1 0 1 0 1 off
0 0 1 1 0 off
1 0 0 0 0 off
1 1 0 0 1 on
1 0 1 0 1 on
1 1 0 0 1 on
1 1 0 0 1 on
0 0 0 1 0 off
0 0 1 0 0 off
0 0 1 0 0 off
0 0 1 1 0 off

Sign in to comment.

1 Answer

Answer by jonas
on 23 Jul 2018
Edited by jonas
on 24 Jul 2018
 Accepted Answer

If you have the image processing toolbox, you can use the function bwconncomp to find connected 1's. Using the example in the comments, this script finds segments where there are 3 or more consecutive 1's, and fills the output vector with 1's (indicating 'on')
CC=bwconncomp(M(:,5),4)
size(CC.PixelIdxList)
for i=1:numel(CC.PixelIdxList)
if numel(CC.PixelIdxList{i})>=3
out(CC.PixelIdxList{i})=1
end
end
If you do not have the image processing toolbox, you can use this solution instead.
%%Find at least 3 consecutive 1's
target=ones(1,3);
pos = strfind(M(:,5)',target);
out=zeros(size(M(:,5)));
for i=1:numel(pos)
out(pos(i):pos(i)+2)=1;
end
You can change the target to any number of consecutive 1's. You can also find other patterns, such as [1 0 1], but you will have to tweak the for-loop a bit.

  4 Comments

Show 1 older comment
jonas
on 24 Jul 2018
No problem! I've updated the solution
JamJan
on 24 Jul 2018
Thanks!!! However, it seems that it places 4 ones when it sees 4 consecutive ones? I think it then should be like this
%%Find at least 3 consecutive 1's
target=ones(1,3);
pos = strfind(M(:,5)',target);
out=zeros(size(M(:,5)));
for i=1:numel(pos)
out(pos(i):pos(i)+2)=1;
end
Anyway, thanks this helps a lot!
jonas
on 24 Jul 2018
Ah yes, that's correct. Updated the solution!

Sign in to comment.