MATLAB: Extracting subset of rows from columns in multidimensional matrix

matrix array

Hi, I have a 5269x7x1526 matrix/tensor and I need to extract data based on criteria from column 5 of the middle index. When extracting this data I need to keep the data entries from columns 5 and 2 together because I will need to plot them. For example, column 2 of the middle dimension of my matrix/tensor is temperature data and column 5 is location data. So far this is what I have:
for i = 1:num_time_steps
range = T_all((T_all(:,5,i)>=0.02) & (T_all(:,5,i)<=0.03),:);
end
Here I am trying to extract data associated with column 5 that is between 0.02 and 0.03 but when I run this loop I get a bunch of zeros out except for the last 10 or so columns which do not give me meaningful information.
Does anybody have any ideas of how I can extract paired data that lies between 0.02 and 0.03 of the 5th column of my multidimensional matrix/tensor? I need to keep the location and the temp data together, columns 5 and 2.

Best Answer

I think this is what you're after.
You are on the right track. In the loop below 'idx' is a logical vector identifying all rows where column 5 is between your bounds. Use that index to extract rows of columns 5 and 2.
for i = 1:num_time_steps
idx = (T_all(:,5,i)>=0.02) & (T_all(:,5,i)<=0.03);
pair1 = T_all(idx, 5, i);
pair2 = T_all(idx, 2, i);
end
Alternatively if you'd like the paired values in 1 matrix
for i = 1:num_time_steps
idx = (T_all(:,5,i)>=0.02) & (T_all(:,5,i)<=0.03);
pairs = T_all(idx, [5,2], i)
end