Need help selecting the max of array A, then selecting corresponding array B

조회 수: 14 (최근 30일)
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.

채택된 답변

Wayne King
Wayne King 2012년 9월 18일
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
Michael
Michael 2012년 9월 18일
YES! Thank you!
I thought the 's' was required when you were calling a string?
Wayne King
Wayne King 2012년 9월 18일
No, here the string is taken care of with your %s

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Babak
Babak 2012년 9월 18일
At the end of your code add these lines:
[maxPE,index] = max(pe);
countryNameMAX = cn(index)
maxPE and countryNameMAX give you what you need.
  댓글 수: 1
Michael
Michael 2012년 9월 18일
[maxpe,index] = max(pe);
countrynameMAX=cn{index};
fprintf('\n The country with the highest percentage of wind energy is %s with %.2f',countrynameMAX,maxpe,'s')
%Calculating minimmum value of PE, indexing, then using index to find
%corresponding country value.
[minpe,index] = min(pe);
countrynameMIN=cn{index};
fprintf('\n The country with the lowest percentage of wind energy is %s with %.2f',countrynameMIN,minpe,'s')
But the output is;
<<The country with the highest percentage of wind energy is Spain with 92.25
The country with the highest percentage of wind energy is s with
The country with the lowest percentage of wind energy is sweoi with 3.67
The country with the lowest percentage of wind energy is s with
The average percent of wind energy for all 4 countries is 58.98
What happened?
You guys were both really quick to reply. I think it's awesome and really appreciate the help.

댓글을 달려면 로그인하십시오.


Wayne King
Wayne King 2012년 9월 18일
편집: Wayne King 2012년 9월 18일
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
  댓글 수: 1
Michael
Michael 2012년 9월 18일
[maxpe,index] = max(pe);
countrynameMAX=cn{index};
fprintf('\n The country with the highest percentage of wind energy is %s with %.2f',countrynameMAX,maxpe,'s')
%Calculating minimmum value of PE, indexing, then using index to find
%corresponding country value.
[minpe,index] = min(pe);
countrynameMIN=cn{index};
fprintf('\n The country with the lowest percentage of wind energy is %s with %.2f',countrynameMIN,minpe,'s')
But the output is;
<<The country with the highest percentage of wind energy is Spain with 92.25
The country with the highest percentage of wind energy is s with
The country with the lowest percentage of wind energy is sweoi with 3.67
The country with the lowest percentage of wind energy is s with
The average percent of wind energy for all 4 countries is 58.98
What happened?
You guys were both really quick to reply. I think it's awesome and really appreciate the help.

댓글을 달려면 로그인하십시오.


Michael
Michael 2012년 9월 18일
편집: Michael 2012년 9월 18일
[maxpe,index] = max(pe);
countrynameMAX=cn{index};
fprintf('\n The country with the highest percentage of wind energy is %s with %.2f',countrynameMAX,maxpe,'s')
%Calculating minimmum value of PE, indexing, then using index to find
%corresponding country value.
[minpe,index] = min(pe);
countrynameMIN=cn{index};
fprintf('\n The country with the lowest percentage of wind energy is %s with %.2f',countrynameMIN,minpe,'s')
But the output is;
<<The country with the highest percentage of wind energy is Spain with 92.25
The country with the highest percentage of wind energy is s with
The country with the lowest percentage of wind energy is sweoi with 3.67
The country with the lowest percentage of wind energy is s with
The average percent of wind energy for all 4 countries is 58.98
What happened?
You guys were both really quick to reply. I think it's awesome and really appreciate the help.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by