Max of cells. Keep whole Column
1 view (last 30 days)
Show older comments
Hello Buddies, I have a table that looks like the one I attached. I would like to find maximum of a specific cell (horsepower), for each second Column. And then Return the Best 3 (or sometimes best 4, 5, 6... as well) of all the Columns to a result table. It should contain the left info column as well to each of the 'winners' (max's). The table can have more columns (even numbers since it pairs two) and do not necessarily have to have exact number of rows (as shown).. The table looks exact as attached. Anyone have some idea to handle this problem?
Hope to hear from someone, -Best Martin
0 Comments
Accepted Answer
Geoff Hayes
on 27 Jun 2015
Martin - you could try iterating over each pair of columns and store the value of the horsepower variable (if it exists) along with the column index in a separate array then do some sort of sort afterwards to extract the top three, four, or five variables.
The following isn't tested but you should be able to adopt it for your needs
% an array for the horsepower value and column index
hpValueIdx = [];
for k=1:2:size(myData,2)
% determine the rows of the kth column that have the horsepower string
hpIdx = strcmp(myData(:,k),'horsepower');
if any(hpIdx)
hpValue = myData{hpIdx,k+1};
hpValueIdx = [hpValueIdx ; [str2num(hpValue) k]];
end
end
The above code assumes that there is only one horsepower row per column and the horsepower values are strings (which your attached image seems to suggest).
After the *for* loop has completed its iterations, you can then sort the *hpValueIdx* 2-D array on the horsepower column as
sortrows(hpValueIdx,1)
The result will be sorted in ascending order so you can just flip it (see flipud) to get the first column sorted in descending order. You can then choose how many (top three, four or five) that you wish to extract to create your results array.
3 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!