select column X values across rows based on meeting multiple conditions from column Y and Z in 3D matrix and then importing those values into a 2D matrix

3 views (last 30 days)
I have the following code that successfully finds .txt files for multiple subjects, and imports them into a 3d matrix. Each summary file is 64 rows and 41 columns.
clc
clear all
%be in correct directory where your subject folders will live
cd /Users/mmar/Desktop/results/test %this will be different for you
% look in each folder starting with "S*" and load in
f = dir(strcat('S*'));
[subjects, jj]=size(f);
%this loads in 3d matrix 64x41xi 3d matrix where i is the number of
%subjects with summary_starttrial1_.txt files
for i = 1:subjects
sub = f(i).name;
sumfile(:,:,i) = load (strcat(sub,'/',sub,'summary_starttrial1_.txt'));
end
I am having trouble with the following next steps:
1) I would like to pull out from each of my matrices from multiple columns based on if two conditions are met: for each subject (in the 3rd dimension), if column 2 of summary_starttrial1.txt == 1 (32/64 rows this will be true within a subject), and column 3 of summary_starttrial1.txt ==1 (this will always be 1 or 2 or 3 or 4 across all rows with each summary trial txt file), then send values from column 14 to a new matrix - repeat this for each subject and then squeeze the col 14s from the 3D matrix into a 2D matrix with 32 rows and columns where the columns will have the data of subjects who met the two conditions.
2)repeat the above 7 more times where col2==2 and col3==1, where col2==1 and col3==2, where col2==2 and col3==2, where col2==1 and col3==3, where col2==2 and col3==3, where col2==1 and col3==4, where col2==2 and col3=4
3) eventually I'd want to repeat all of that with the exception of selecting values from cols1 5, 17, 21, 22, 23, 24, 25, 28, 29, 32, 33, 35, 36, 37 and importing them into 8 x 3d matrices
If anyone can help with any of these steps that would be super amazing.

Answers (1)

David Hill
David Hill on 9 Feb 2022
An example would be helpful (or attach your matrix), but if I understand you.
yourMatrix=randi(5,64,41,5);%example matrix
newCell=cell(1,5);
for k=1:5
newCell{k}=yourMatrix(yourMatrix(:,2,k)==1&yourMatrix(:,3,k)==1,14);
end
Once you get the newCell, not sure what you want to do. I did not understand your sqeezing statement (hard to sqeeze with different length arrays).
  1 Comment
meechellevdm
meechellevdm on 9 Feb 2022
Edited: meechellevdm on 9 Feb 2022
Thanks so much David - your code is definitely getting me a lot further than what I have managed on my own - I really appreciate it.
I have attached the sumfile.mat (it has 64 rows x 41 columns x 45 subjects)
I have gone ahead and followed your instruction code as follows
newCell=cell(1,45);
for k=1:45
led1cm1{k}=sumfile(sumfile(:,2,k)==1&sumfile(:,3,k)==1,14);
led1cm2{k}=sumfile(sumfile(:,2,k)==2&sumfile(:,3,k)==1,14);
led2cm1{k}=sumfile(sumfile(:,2,k)==1&sumfile(:,3,k)==2,14);
led2cm2{k}=sumfile(sumfile(:,2,k)==2&sumfile(:,3,k)==2,14);
led3cm1{k}=sumfile(sumfile(:,2,k)==1&sumfile(:,3,k)==3,14);
led3cm2{k}=sumfile(sumfile(:,2,k)==2&sumfile(:,3,k)==3,14);
led4cm1{k}=sumfile(sumfile(:,2,k)==1&sumfile(:,3,k)==4,14);
led4cm2{k}=sumfile(sumfile(:,2,k)==2&sumfile(:,3,k)==4,14);
end
This gives me 8 new array cells, each with k entries, but each of those array cells won't have 45 full entries, with [] in places where there is no data - this is expected. However, I don't think it is giving me the right values that meet both conditions - for example, when I open led1cm1, it it is giving me the same values in 32x1 arrays multiple times instead of checking and transferring the column values from the different summary text files.
Once that is fixed, what I would want to do next
  1. Nuke the columns in the cell array with no data []
  2. repeat the above steps except grab column 15, 17, 22, etc in the same way as column 14
  3. somehow end up with a 3D matrix for each of these 8 conditions, with the 3rd dimension varying by the column of values extracted based on the two conditions being met e.g. led1cm(j,i,k) where j will always be 32, i would the number of subjects, and k would be the column. I can clarify more if that didn't make sense.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!