for loop for 3D structural array
4 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
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.)
0 Comments
See Also
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!