for loop for 3D structural array

4 views (last 30 days)
Lauren
Lauren on 10 Sep 2014
Commented: Star Strider on 10 Sep 2014
Hi Everyone,
I have a 13x13x6 structural array and would like to extract one of its fields and put it into another variable. I am recording local field potential data from rat and this animal in particular has 13 recording channels. This matrix involves computing the coherence between all possible channel combinations where the diagonal of the matrix equals 1 (coherence of a channel with itself) and the upper and lower halves of the 13x13 matrix are mirror images of each other. The third dimension involves coherence over time where each channel combination has 6 blocks (e.g., 1,2,1...1,2,6). I would like to extract only the upper half of the diagonal, as the diagonal and the lower half are not relevant for me. Basically, I would like to do the following but would like an easier way to do it:
XX(1,1)=C(1,2,1).Cxy;
XX(1,2)=C(1,2,2).Cxy;
XX(1,3)=C(1,2,3).Cxy;
XX(1,4)=C(1,2,4).Cxy;
XX(1,5)=C(1,2,5).Cxy;
XX(1,6)=C(1,2,6).Cxy;
XX(2,1)=C(1,3,1).Cxy;
XX(2,2)=C(1,3,2).Cxy;
XX(2,3)=C(1,3,3).Cxy;
XX(2,4)=C(1,3,4).Cxy;
XX(2,5)=C(1,3,5).Cxy;
XX(2,6)=C(1,3,6).Cxy;
% Where XX is the new variable that I would like to create and C is the pre-existing 13x13x6 struct. array matrix. Cxy is a field in C and for each channel pairing (e.g. 1,2) there are 6 blocks (e.g., 1,2,1 through 1,2,6).
I think for this animal there are 78 relevant channel combinations (upper half of matrix diagonal) and each has 6 blocks so the new XX matrix would end up being a 78X6. I would like to make this loop as versatile as possible as the C matrix will vary quite a bit across animals (for example, if the animal has more than 13 recording channels and more blocks). So while this struct. array matrix is a 13x13x6 for this animal, for the next animal the matrix may be a 9x9x10.
I hope this makes sense and thank you so much in advance!
Best, Lauren

Answers (2)

Star Strider
Star Strider on 10 Sep 2014
The triu function may be what you’re looking for. (You’ll probably need struct2cell and cell2mat before that, though.)

Lauren
Lauren on 10 Sep 2014
Thank you for you comment.
I tried to implement what you suggested and I'm having some issues. First I had to remove fields from my 13x13x6 structural array matrix so I'm left just with the field Cxy, which was fine (I used rmfield). Then I used struct2cell and then cell2mat as you suggested, but instead of being left with a matrix, i'm left with a 4-D double. Perhaps I didn't fully explain my initial variable, C, properly. So I'll try again:
If I type into the command line C, matlab tells me C is a 13x13x6 struct array with fields: A, B, C, D, E F. Now if I ask matlab: C(1,1,1) it tells me field A: 1; B: 3.4...etc (i'm just making up field names and values). So, for example, I'm particularly interested in field F. So
C(1,1,1).F = 1
C(1,1,2).F = 1
C(1,1,2).F = 1 (the answers are all 1's because channel 1 is being correlated with itself)
The third dimension is the time blocks where C(1,1,1).F = 1 is the coherence between channel 1 and itself evaluated at time point 1; C(1,1,2) = 1 is coherence between channel 1 and itself evaluated at time point 2...all the way to time point 6. Now, for each channel pairings (except for the diagonal-- e.g., (1,1); (2,2); (3,3), etc) I want to take all of those 6, single time point values and put them into a matrix such that each row will be the relevant channel pairing (1,2) and each column will be each time point (1,2,1....1,2,6).
I am attaching a matlab workspace that had the variable I am working with so you can visualize the structure of the variable. The variable is called Area20, and I would like to extract field NormA05 from Area20. Like I said in my previous post, the code should do this (I changed the variable and field to be the same as what I sent in the workspace:
XX(1,1)=Area20(1,2,1).NormA05;
XX(1,2)=Area20(1,2,2).NormA05;
XX(1,3)=Area20(1,2,3).NormA05;
XX(1,4)=Area20(1,2,4).NormA05;
XX(1,5)=Area20(1,2,5).NormA05;
XX(1,6)=Area20(1,2,6).NormA05;
XX(2,1)=Area20(1,3,1).NormA05;
XX(2,2)=Area20(1,3,2).NormA05;
XX(2,3)=Area20(1,3,3).NormA05;
XX(2,4)=Area20(1,3,4).NormA05;
XX(2,5)=Area20(1,3,5).NormA05;
XX(2,6)=Area20(1,3,6).NormA05;
XX will end up being XX(78,6). Where the first and second dimension in C (really Area20) are the channel pairings (1,2; 1,3, all the way to 1,13), then (2,3; 2,4...all the way to 13)...(12,13) should be the last pairing. If you copy and paste this code into the command like you will see what you have a 2x6 matrix with coherence values over the 6 time points for channel pairing (1,2)-- row 1 and channel pairing (1,3)-- row 2. If you also type in Area20(1,2,1) that will be (1,1) in XX; Area20(1,2,2) will be (1,2) in XX, etc.
I hope this is more clear. Again, thank you for your comments and I'm looking forward to your (and anyone else's) response.
Thanks a lot, Lauren
  2 Comments
Lauren
Lauren on 10 Sep 2014
I actually figured out what I needed to do-- although it may not be the most efficient, it does the job:
for k=1:length(Area20)-1
for n=k+1:length(Area20)
for m=1:size(Area20,3)
if ~exist ('y')
y=1;
NormA05(y,m)=Area20(k,n,m).NormA05;
else
NormA05(y,m)=Area20(k,n,m).NormA05;
end
end
y=y+1;
end
end
Thanks again Star Strider for your comments.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!