Pairing row indices with multiple tables
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Cris LaPierre
2021-8-11
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.
回答(2 个)
Cris LaPierre
2021-8-12
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!