# How can i find row and column numbers of max value of each field in structure?

3 views (last 30 days)
Kiryl Zhaliazka on 8 Mar 2022
Commented: Kiryl Zhaliazka on 8 Mar 2022
I fave struct s with 3 fields that have different names, each field 5*10 matrix. Also I have veriable s_max 3*1 with max value from each field.
How can i create a new two variables:
1) s_row with rows numbers corresponding to s_max from each fied
2) s_col with column numbers corresponding to s_max from each fied
Thank you!

Davide Masiello on 8 Mar 2022
Edited: Davide Masiello on 8 Mar 2022
clear,clc
s.ciao = rand(5);
s.hello = rand(5);
s.hola = rand(5);
s.max = [max(s.ciao(:));max(s.hello(:));max(s.hola(:))];
names = fields(s);
for i = 1:length(names)-1
[s.row(i,1),s.col(i,1)] = find(s.(names{i}) == s.max(i));
end
The loop is a bit of an overkill for only three fields, but the code above will work fine for any number n of fields, just make sure that s.max is the last one.
s =
struct with fields:
ciao: [5×5 double]
hello: [5×5 double]
hola: [5×5 double]
max: [3×1 double]
row: [3×1 double]
col: [3×1 double]
>> s.row
ans =
3
2
3
>> s.col
ans =
4
2
4
Kiryl Zhaliazka on 8 Mar 2022
thank you !