Read from an Excel File and assign index that have the max/min value

3 vues (au cours des 30 derniers jours)
sohaib bakr
sohaib bakr le 4 Jan 2020
Modifié(e) : dpb le 5 Jan 2020
Hello Evreyone >
If i am reading from an Excel file and let suppose that the Excel file contains 36 columns and 22 rows and i want to find the maximum for that table with its Location
I know how to find the maximum value but the thing that i don't know is how to get it is location of (row,column) !!!
for example let suppose that the maximum =0.77775 and it located
at Column =10 and Row = 12 i want it to give me the result like that:
the maximum =0.77775 at location Column =10 , Row = 12 .
and this is my code :
clc
clear all
Leakage_power_matrix = xlsread ('leakged_power_with_windows.csv') ;
Max_leakage_power = max(max(Leakage_power_matrix)) ;
fprintf('Maximum Leakage power is equal to= %f DB\n', Max_leakage_power ) ;
Regards.

Réponse acceptée

dpb
dpb le 4 Jan 2020
Modifié(e) : dpb le 5 Jan 2020
Use the optional returned location variable and ind2sub to convert the position to coordinates. See the documentation; it shows examples.
[Max_leakage_power,loc]=max(Leakage_power_matrix(:));
[r,c]=ind2sub(size(Leakage_power_matrix),loc);
fmt='The maximum =%0.5f at location Column = %d, Row = %d.\n';
fprintf(fmt,Max_leakage_power,r,c);
Just noticed to refresh my memory of order of inputs; ind2sub should be in the See Also list for min,max
BTW, can eliminate the need for the (:) idiom if have R2018 or later by using the optional 'all' argument to return the global maximum.
NB: ERRATUM
The order of the row,column outputs are reversed relative to the format string in the order you wrote them as column, row...normally the row is given first and I just automatically wrote them in that order in the fprintf argument list...
Either swap them there or fix the format string to match the present order.
  3 commentaires
dpb
dpb le 4 Jan 2020
My bad...it's ind2sub you want here; not sub2ind. I wrote the correct one in the text but the wrong one in the code...
sohaib bakr
sohaib bakr le 4 Jan 2020
Thanks alot >>> it's works .

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by