Indexing into a matrix using another matrix within a loop

94 Views Asked by At

So I have a matrix named "all_sds" sized 5x5x54 where the 5x5 matrix is symmetric around the diagonal and the 54 represents the number of subjects. The number 5 represents a variable so the 5x5 part is basically a symmetric matrix of each variable correlated with the other; there are 10 comparisons total. I have made a 54x10 matrix to store these 10 comparison values for each participant using the following code where nSub = 54, num_comparisons = 10, nROI = 5:

sub_sds = zeros(nSub,num_comparisons);

I then used the following code to get a 10x2 matrix of possible combination values of the numbers 1-5.

comps = flipud(combnk(1:nROI,2));

What I want to do is populate the matrix "sub_sds" with the appropriate values from "all_sds" so the first subject variable1xvariable2 comparison value will go in row1 col1 of the "sub_sds" variable and the first subject variable1xvariable3 comparison value will go in row1 col2, etc. I made the variable "comps" because I thought I could index into "all_sds" using its values but I now realized I have no idea how to do that appropriately. I have been using a nested loop for value assignation and this was as far as I got.

for qq = 1:nSub
    for ww = 1:num_comparisons
        sub_sds(qq,ww) = all_sds(**something with variable comps**,qq); 
    end
end

Clearly I have no idea how to index into a matrix using another matrix...plus it's in a loop so I'm doubly confused. Can anybody point me in the right direction as to how to do this? Thanks so much.

1

There are 1 best solutions below

0
On

You can use

sub_sds = zeros(nSub,num_comparisons);
for i=1:size(all_sds,3)
    sub_sds(i,:) = nonzeros(triu(all_sds(:,:,i)',1));
end