Need help selecting the max of array A, then selecting corresponding array B
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
n=input('How many countries will be evaluated? ')
for k=1:1:n
%cn=country name
cn(k)={input('Enter Country name: ','s')};
%we=wind energy
we(k)=input('Enter Generated Wind Energy, in TWh ');
%te=total energy
te(k)=input('Enter Total Electrical Energy, in TWh ');
%pe=percent of energy
pe(k)=we(k)*100/te(k);
end
So in this case the two important arrays are pe and cn, or percent of energy and country name.
I need the program to evaluate the array pe and find the max; then find the country name that it corresponds too. I'm really at a loss for how to accomplish this!
Any help/insight is appreciated.
0 commentaires
Réponse acceptée
Wayne King
le 18 Sep 2012
The problem is the 's' at the end of your fprintf() statement
printf('\n The country with the lowest percentage of wind energy is %s with %2.2f \n',countrynameMIN,minpe)
2 commentaires
Plus de réponses (3)
Babak
le 18 Sep 2012
At the end of your code add these lines:
[maxPE,index] = max(pe);
countryNameMAX = cn(index)
maxPE and countryNameMAX give you what you need.
Wayne King
le 18 Sep 2012
Modifié(e) : Wayne King
le 18 Sep 2012
What about just
[maxval,index] = max(pe);
cn{index}
By using cn{index} you get the country back as a string -- a character array. Using
cn(index)
you get the country back as a 1x1 cell array
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices 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!