# Find maximum for each Image in table

1 view (last 30 days)
Pouyan Sadeghian on 6 Jun 2021
Good evening,
I have a similar issue like my previous question (For Loop Table Matlab - MATLAB Answers - MATLAB Central (mathworks.com)).
I have a small excerpt of my table. In the coloumn Label you see my Images and in the column MaxPosition and Intenisty my measurements.
Number Label MaxPosition Intensity
______ __________________________ ___________ ________
1 {'Image0100 16-28-31.bmp'} 94 10
2 {'Image0100 16-28-31.bmp'} 123 8
3 {'Image0101 16-28-46.bmp'} 95 23
4 {'Image0101 16-28-46.bmp'} 124 43
5 {'Image0102 16-29-01.bmp'} 95 22
6 {'Image0102 16-29-01.bmp'} 125 54
7 {'Image0103 16-29-16.bmp'} 96 94
8 {'Image0103 16-29-16.bmp'} 126 30
9 {'Image0104 16-29-31.bmp'} 98 49
10 {'Image0104 16-29-31.bmp'} 127 39
11 {'Image0105 16-29-46.bmp'} 98 20
12 {'Image0105 16-29-46.bmp'} 128 10
13 {'Image0106 16-30-01.bmp'} 98 2
14 {'Image0106 16-30-01.bmp'} 128 94
15 {'Image0107 16-30-16.bmp'} 99 93
16 {'Image0107 16-30-16.bmp'} 129 54
17 {'Image0108 16-30-31.bmp'} 100 32
18 {'Image0108 16-30-31.bmp'} 130 44
Now I want to find the max of the Intensity for every Image with the corresponding MaxPosition.
I use varfun but this command gives me the max of the MaxPosition and the max of the Intensity of every Image, not the corresponding one.
For example for Image0100 the max Intensity is 10 and the correpsonding to it is 94. And I want to display 94 in my new table.
Thanks.
Best
Pouyan
Walter Roberson on 7 Jun 2021
findgroups() splitapply()
Pouyan Sadeghian on 7 Jun 2021
Edited: Pouyan Sadeghian on 7 Jun 2021
thank you for your response. I tried to understand these commands and tried to use them but I think it does not solve my problem.
Maybe my question is not clear.
l have for every image multiple measurements for Intensity and for MaxPosition. What I need is the maximum value of the Intensity for each image with the corresponding MaxPosition value to the Intensity.
I think what I get with your commands is the maximum value of Intensity and the maximum value of MaxPosition for each image. But I do not need this.
Or maybe I use these commanda false.
I hope you could understand my question better.
Best

Duncan Po on 7 Jun 2021
You can use groupsummary to find the maximum value of each group:
T1 = groupsummary(T, 'MaxPosition', 'max', 'Intensity');
Walter Roberson on 7 Jun 2021
Even better!
Pouyan Sadeghian on 7 Jun 2021
Thanks @Duncan Po for your response.
Its a very easy and quick command.
I was searching about a command like this. I used varfun but it did not solve my problem.
Best
Pouyan

Walter Roberson on 7 Jun 2021
data = {
1 {'Image0100 16-28-31.bmp'} 94 10
2 {'Image0100 16-28-31.bmp'} 123 8
3 {'Image0101 16-28-46.bmp'} 95 23
4 {'Image0101 16-28-46.bmp'} 124 43
5 {'Image0102 16-29-01.bmp'} 95 22
6 {'Image0102 16-29-01.bmp'} 125 54
7 {'Image0103 16-29-16.bmp'} 96 94
8 {'Image0103 16-29-16.bmp'} 126 30
9 {'Image0104 16-29-31.bmp'} 98 49
10 {'Image0104 16-29-31.bmp'} 127 39
11 {'Image0105 16-29-46.bmp'} 98 20
12 {'Image0105 16-29-46.bmp'} 128 10
13 {'Image0106 16-30-01.bmp'} 98 2
14 {'Image0106 16-30-01.bmp'} 128 94
15 {'Image0107 16-30-16.bmp'} 99 93
16 {'Image0107 16-30-16.bmp'} 129 54
17 {'Image0108 16-30-31.bmp'} 100 32
18 {'Image0108 16-30-31.bmp'} 130 44};
T = cell2table(data, 'VariableNames', {'Number', 'Label', 'MaxPosition', 'Intensity'});
T(1:5,:)
ans = 5×4 table
Number Label MaxPosition Intensity ______ __________________________ ___________ _________ 1 {'Image0100 16-28-31.bmp'} 94 10 2 {'Image0100 16-28-31.bmp'} 123 8 3 {'Image0101 16-28-46.bmp'} 95 23 4 {'Image0101 16-28-46.bmp'} 124 43 5 {'Image0102 16-29-01.bmp'} 95 22
[G, ID] = findgroups(T.Label);
results = cell2mat(splitapply(@findmax, T.MaxPosition, T.Intensity, G));
output = table(ID, results(:,1), results(:,2), 'VariableNames', {'Label', 'Intensity', 'MaxPosition'});
output
output = 9×3 table
Label Intensity MaxPosition __________________________ _________ ___________ {'Image0100 16-28-31.bmp'} 10 94 {'Image0101 16-28-46.bmp'} 43 124 {'Image0102 16-29-01.bmp'} 54 125 {'Image0103 16-29-16.bmp'} 94 96 {'Image0104 16-29-31.bmp'} 49 98 {'Image0105 16-29-46.bmp'} 20 98 {'Image0106 16-30-01.bmp'} 94 128 {'Image0107 16-30-16.bmp'} 93 99 {'Image0108 16-30-31.bmp'} 44 130
function results = findmax(Pos, Inten)
[maxinten, idx] = max(Inten);
results = {maxinten, Pos(idx)};
end
Walter Roberson on 7 Jun 2021
@Duncan Po's response is better than mine for this purpose !
Pouyan Sadeghian on 7 Jun 2021
alright.
I used your code and It satifies my problem but youre right. His response is shorter and easier.
Thank you

Sulaymon Eshkabilov on 7 Jun 2021
A = ... % Table of all data
B = A.MaxPosition; % Separate out the column
Pouyan Sadeghian on 7 Jun 2021
thank you for your response. Maybe you dont understand my problem or I explain it not clearly.
I want to find the max value of the Intensity for each image with the corresponding MaxPosition.
For example the maximum Intensity for image0101 is 43 (line 4) and I want also the corresponding MaxPosition to the maximum Intensity which is 124.
Maybe this is more understandable.