Pairing row indices with multiple tables

1 view (last 30 days)
I have one vector of row indeces row= 2 1 2 2 12 21 22 13 19 1 2 4 3 4 1 1, these tell me the rows associated with a max value. I have a vector of 16 tables called databin. I'm trying to associate the row indeces with the tables. For example for the first row: row indece 2 for table 1, row indece 1, from table 2, row indece 2 from table 3. etc... and then put all the data associated from the row indeces and put it in a new table. The problem is, in my for loop, it just gives repeating the row indece for the first table , For example it gives me the row indece 2 for the first table, row indece 1 for the first table, row indece 2 for the first table-- that's not what I want. Can someone help me with this??
  1 Comment
Cris LaPierre
Cris LaPierre on 11 Aug 2021
There may be an easier way, but I would want to start from the orginal 16 tables. Save your tables to a mat file and attach it to your post using the paperclip icon.

Sign in to comment.

Answers (2)

Jacqueline Chrabot
Jacqueline Chrabot on 11 Aug 2021
This is my list of tables.
  2 Comments
Jacqueline Chrabot
Jacqueline Chrabot on 12 Aug 2021
Cris,
this is my for loop for right now and shows how I got the max value I needed. The only line that isn't really working is the one that is % out (the last line of the loop). That's where I'm trying to get the rows associated with the max value and pair the indeces with the tables.
Varnum1=11 %chloro
Varnum7=7 %depth
Varnum=1 %time
maxchloro=ones(1,length(databin));
minchloro=ones(1,length(databin));
meanchloro=ones(1,length(databin));
stdev=ones(1,length(databin))
for j=1:length(databin)
temp2=databin{j};
maxchloro(:,j)=max(temp2(:,Varnum1))
disp('all good until here')
indexesOfMax(:,j) = find(temp2(:)==maxchloro) %give the indeces of the maximum value, you can repeat this code for upper and lower bounds
[row,col] = ind2sub(size(temp2),indexesOfMax) %rows9_9_9_10=1 2 19 17 22 3 2 1 3 1 2 1 contain those where the chlorophyll maximums are found
% ssTable= table(temp2(row,:)') % this gives you the table but it just repeats the table indeces if they duplicate
end

Sign in to comment.


Cris LaPierre
Cris LaPierre on 12 Aug 2021
Note that max has a calling syntax that returns the index of the max it finds.
I would use the following code instead of your for loop.
load databin.mat
Varnum1=11 %chloro
Varnum1 = 11
A = cell2mat(reshape(databin,1,1,length(databin)));
[val,ind] = max(squeeze(A(:,Varnum1,:)),[],'omitnan')
val = 1×16
47.0333 35.0929 263.4783 95.0125 105.2000 45.6800 58.1737 46.5190 387.7714 79.3000 148.9308 16.7846 77.0182 22.4167 121.2704 420.3650
ind = 1×16
2 1 2 2 12 21 22 13 19 1 2 4 3 4 1 1
As for obtaining a final 2D matrix of just the rows corresponding to the max values of chloro, that can be accomplished with some reshaping magic.
r = ind + [0,cumsum(repmat(size(A,1),1,size(A,3)-1))];
B = cell2mat(databin);
B = B(r,:)
B = 16×13
1.0e+05 * 7.3656 NaN 0.0003 0.0003 0.0002 0.0000 0.0000 0.0001 0.0001 0.0004 0.0005 0.0012 0.0001 7.3656 NaN 0.0003 0.0003 0.0002 0.0000 0.0000 0.0001 0.0021 0.0003 0.0004 0.0012 0.0001 7.3656 NaN 0.0003 0.0004 0.0002 0.0000 0.0000 0.0001 0.0001 0.0027 0.0026 0.0025 0.0002 7.3656 NaN 0.0003 0.0004 0.0002 0.0000 0.0000 0.0001 0.0001 0.0009 0.0010 0.0017 0.0001 7.3656 NaN 0.0003 0.0004 0.0002 0.0000 0.0000 0.0001 0.0001 0.0011 0.0011 0.0014 0.0001 7.3656 NaN 0.0003 0.0004 0.0002 0.0001 0.0001 0.0001 0.0001 0.0004 0.0005 0.0013 0.0001 7.3656 NaN 0.0003 0.0004 0.0002 0.0001 0.0001 0.0001 0.0002 0.0006 0.0006 0.0011 0.0001 7.3656 NaN 0.0003 0.0003 0.0002 0.0000 0.0000 0.0001 0.0002 0.0004 0.0005 0.0009 0.0001 7.3656 NaN 0.0003 0.0004 0.0002 0.0000 0.0000 0.0001 0.0003 0.0041 0.0039 0.0007 0.0000 7.3656 NaN 0.0003 0.0004 0.0002 0.0000 0.0000 0.0001 0.0001 0.0008 0.0008 0.0008 0.0001
% Just to verify that the values in column 11 are the max values
B(:,Varnum1)
ans = 16×1
47.0333 35.0929 263.4783 95.0125 105.2000 45.6800 58.1737 46.5190 387.7714 79.3000

Categories

Find more on Tables 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!