find mpg of lightest car

조회 수: 4 (최근 30일)
C KAMALESWAR REDDY
C KAMALESWAR REDDY 2021년 8월 2일
댓글: C KAMALESWAR REDDY 2021년 8월 2일
function mpg = sort_cars(N)
wgt=[cars.Weight]' ;
mpg1=[cars.MPG]';
mpg2=zeros(1,length(wgt));
for i=1:length(wgt)
[wgt1 inx]=max(wgt);
mpg2(end-i+1)=mpg1(inx);
wgt(inx)=0;
end
mpg = mpg2(1:N);
end
this is my code for the problem 44952 but it is not executing..showing error
Below is one of the test case..for the above mentioned problem..
N = 5
load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carbig.mat'));
Model = strtrim(string(Model));
cars = table(Model, MPG, Horsepower, Weight, Acceleration);
save cars.mat cars
assert(isequal(sort_cars(N),[35; 31; 39.1; 35.1; 31]));
can anyone plz help me in solving this problem...
Thank you

답변 (1개)

Walter Roberson
Walter Roberson 2021년 8월 2일
When you load data into the base workspace like you do, then the variable cars will not be sured with the inside of the function. It is possible to share variables into a function, but only for nested functions.
  댓글 수: 1
C KAMALESWAR REDDY
C KAMALESWAR REDDY 2021년 8월 2일
sorry but which part should i need to edit or change

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by