Pairing row indices with multiple tables
1 view (last 30 days)
Show older comments
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
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.
Answers (2)
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
A = cell2mat(reshape(databin,1,1,length(databin)));
[val,ind] = max(squeeze(A(:,Varnum1,:)),[],'omitnan')
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,:)
% Just to verify that the values in column 11 are the max values
B(:,Varnum1)
0 Comments
See Also
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!