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

25 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Davide Masiello
Davide Masiello le 8 Mar 2022
Modifié(e) : Davide Masiello le 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

Plus de réponses (0)

Catégories

En savoir plus sur Chemistry dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by