Reputation: 71
I have a rather large matrix in which column 1 list times in 100ths of a sec (e.g. 3000 = 30s) and in column 2 are event codes (e.g. 1 = start of trial, 2 = start of response, 4 = end of trial). I have used this larger matrix to determine all sorts of indices. However, now I need to compute an indice on a trial by trial basis (i.e. based on the values between 1 and 4 which repeat for a total of 60 times. An example array consisting of 3 trials is below:
0 1
682 2
987 3
2586 2
2593 3
2598 2
2601 3
2602 2
2605 3
2607 2
2608 3
2635 2
2636 3
5546 4
7321 1
7826 2
7900 3
7901 2
7902 3
9481 2
9730 3
9877 2
10319 3
10431 4
11158 1
11361 2
11376 3
12209 2
12267 3
13547 2
14159 4
What I want to do is populate a new array for each 1 & 4 pair, for example, in this array of 3 x 1 & 4 pairs the first trial would consist of the following
Tarray_1 = [0,682,987,2586,2593,2598, 2601, 2602, 2605, 2607, 2608,2635, 2636, 5546; 1,2,3,2,3,2,3,2,3,2,3,2,3,4];
Is there any easy or straight forward to do this? I've spent the last few days try to use for loops and the find command to determine the indices of every 1 in order to create new arrays. Does anyone have any suggestions as to what I could do?
Upvotes: 2
Views: 132
Reputation: 30579
Given an n-by-2
matrix M
, here is an easy way with just two commands:
>> trialStartEnd = [find(M(:,2)==1) find(M(:,2)==4)]
trialStartEnd =
1 14
15 24
25 31
>> T = arrayfun(@(x,y) M(x:y,:)',trialStartEnd(:,1),trialStartEnd(:,2),'uni',0)
T =
[2x14 double]
[2x10 double]
[2x7 double]
Each trial has it's own array in a cell of T
.
Upvotes: 4
Reputation: 238309
Here is one way, if I understand correctly, of ding this:
A = [0 1;
682 2;
987 3;
2586 2;
2593 3;
2598 2;
2601 3;
2602 2;
2605 3;
2607 2;
2608 3;
2635 2;
2636 3;
5546 4;
7321 1;
7826 2;
7900 3;
7901 2;
7902 3;
9481 2;
9730 3;
9877 2;
10319 3;
10431 4;
11158 1;
11361 2;
11376 3;
12209 2;
12267 3;
13547 2;
14159 4];
% all even codes
evenCodes = A(:, 2);
% find positions of valus 4 in the even cods
[indx c] = find(evenCodes == 4);
% just append 0 to the idx array
newIdxOfFours = [0, indx'];
% cell cointaining Tarrays, e.g. outCell{1} is Tarray_1
TarrayCell = {};
for i = 1:length(newIdxOfFours) - 1
% find starting and ending index/row for each trial
startIdx = newIdxOfFours(i) + 1;
endIdx = newIdxOfFours(i+1);
TarrayCell{end + 1} = A(startIdx:endIdx, :)';
end
TarrayCell{1}
TarrayCell{2}
TarrayCell{3}
>>
ans =
0 682 987 2586 2593 2598 2601 2602 2605 2607 2608 2635 2636 5546
1 2 3 2 3 2 3 2 3 2 3 2 3 4
ans =
7321 7826 7900 7901 7902 9481 9730 9877 10319 10431
1 2 3 2 3 2 3 2 3 4
ans =
11158 11361 11376 12209 12267 13547 14159
1 2 3 2 3 2 4
Upvotes: 0
Reputation: 8459
I wrote this code, which may work with your code, however depending on what you need these matrices to do, having them in cells may be a hassle. But in the absence of other answers this should do the trick. I copied the big matrix you gave and called it A
.
[F,I]=find(A(:,2)==4);
T=cell(1,sum(I));
T{1}=A(1:F(1),:);
for j=2:sum(I)
T{j}=A(F(j-1)+1:F(j),:);
end
This makes a cell array of matrixes, T
, and you can get any matrix out by using eg: T{1}
, or access a particular element with eg: T{1}(2,1)
.
Edit: This assumes that the matrix starts with a 1, and that another 1 always follows a 4. Chappjc's answer is probably better.
Upvotes: 0