I am trying to use if statements to compare two vectors to get a value from a third. I need to display which months of one vector are higher than another then give the months that they are within .2 of each other.

조회 수: 2 (최근 30일)
WSFO =[6.7 7.5 8.5 9.5 10.4 10.9 11.2 10.5 9.1 7.6 6.3 6.5];
WOrl =[9.0 9.6 9.9 9.4 8.8 8.0 7.3 7.2 7.7 8.6 8.6 8.5];
Month={'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Dec'};
A = mean(WSFO);
B = mean(WOrl);
fprintf(['\nThe average annual wind speed in San Francisco was (mph)'...
'\n %f\n'], A);
fprintf(['\nThe average annual wind speed in Orlando was'...
'(mph)\n %f\n'], B);
C = round(sum(WSFO<A));
D = round(sum(WOrl<B));
fprintf(['\nWind speed is below the annual average in San Francisco' ...
' for \n %f months\n'], C);
fprintf(['\nWind speed is below the annual average in Orlando for'...
'\n %f months\n'], D);
E = sum(WSFO > WOrl);
fprintf(['\nWind speed in San Francisco higher than in Orlando '...
'during the %d months of\n'], E);
if WSFO > WOrl
fprintf('%s %s\n',E, Month{1:2:3:4:5:6});
end
F = abs(WSFO - WOrl) <= .2;
if abs(WSFO - WOrl) <= .2
fprintf(['\nWind speed in San Francisco is within ±0.2mph of that in '...
'Orlando during the %d months of\n'], Apr);
end

채택된 답변

KSSV
KSSV 2017년 10월 23일
편집: KSSV 2017년 10월 23일
WSFO =[6.7 7.5 8.5 9.5 10.4 10.9 11.2 10.5 9.1 7.6 6.3 6.5];
WOrl =[9.0 9.6 9.9 9.4 8.8 8.0 7.3 7.2 7.7 8.6 8.6 8.5];
Month={'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Dec'};
A = mean(WSFO);
B = mean(WOrl);
fprintf(['\nThe average annual wind speed in San Francisco was (mph)'...
'\n %f\n'], A);
fprintf(['\nThe average annual wind speed in Orlando was'...
'(mph)\n %f\n'], B);
C = round(sum(WSFO<A));
D = round(sum(WOrl<B));
fprintf(['\nWind speed is below the annual average in San Francisco' ...
' for \n %f months\n'], C);
fprintf(['\nWind speed is below the annual average in Orlando for'...
'\n %f months\n'], D);
%%%%%
E = sum(WSFO > WOrl);
fprintf(['\nWind speed in San Francisco higher than in Orlando '...
'during the %d months of\n'], E);
idx = WSFO > WOrl ;
if E ~= 0
fprintf([repmat('%s,',1,E),'\n'], Month{idx});
end
F = abs(WSFO - WOrl) <= .2;
if abs(WSFO - WOrl) <= .2
fprintf(['\nWind speed in San Francisco is within ±0.2mph of that in '...
'Orlando during the %d months of\n'], Apr);
end
  댓글 수: 3
KSSV
KSSV 2017년 10월 23일
Edited the answer......I changed one condition, you do the other one.
Michael Johnson
Michael Johnson 2017년 10월 23일
That was perfect. Thank you. I got the second part after changing what I needed.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Psychology에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by