Compacting a For Loop

조회 수: 1 (최근 30일)
Benedict Comerford
Benedict Comerford 2020년 10월 7일
댓글: Sindar 2020년 10월 7일
Hi all
I would love some help with compacting a for loop, i'm using it to find the smallest error in a matrix is there a way of finding the smallest value of the matrix as well as the name of the variable associated with the smallest value other then printing out a masive for loop.
Thanks Ben.
  댓글 수: 2
Sindar
Sindar 2020년 10월 7일
  • post code in blocks, don't attach unless it's really long:
norm_er = 1.301;
exp_er = 9.126;
log_er = 1.301;
ray_er = 2.606;
Errors = [norm_er,exp_er,log_er,ray_er]
if norm_er<exp_er && norm_er<log_er && norm_er<ray_er
disp('Normal distribution is best')
elseif exp_er<norm_er && exp_er<log_er && exp_er<ray_er
disp('Exponential distribution is best')
elseif log_er<norm_er && log_er<exp_er && log_er<ray_er
disp('Log distribution is best')
elseif ray_er<norm_er && ray_er<exp_er && ray_er<log_er
disp('Rayleigh distribution is best')
elseif norm_er==exp_er
disp('Normal and Exponential distributions are best')
elseif norm_er==log_er
disp('Normal and Log distributions are best')
elseif norm_er==ray_er
disp('Normal and Rayleigh distributions are best')
elseif exp_er==log_er
disp('Exponential and Log distributions are best')
elseif exp_er==ray_er
disp('Exponential and Rayleigh distributions are best')
elseif log_er==ray_er
disp('Log and Rayleigh distributions are best')
end
  • that's not a for loop
  • is there an actual appreciable chance that two methods will give the same error?
Benedict Comerford
Benedict Comerford 2020년 10월 7일
Hi Sindar
Sorry yeah should have said an if statement
Yes there is a strong possibility that 2 will have the same error

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

채택된 답변

Sindar
Sindar 2020년 10월 7일
편집: Sindar 2020년 10월 7일
norm_er = 1.301;
exp_er = 9.126;
log_er = 1.301;
ray_er = 2.606;
Errors = [norm_er,exp_er,log_er,ray_er];
% set a tolerance to define equal error
tol = 1e-3;
% find minimum error
min_error = min(Errors);
% find all methods with this error value
idxs = find((Errors-min_error) < tol);
error_str = ["Normal";"Exponential";"Log";"Rayleigh"];
% print based on how many errors are equal (extends to any number of distributions)
% none? that's not good
if length(idxs)==0
error('something went wrong')
% e.g., Rayleigh distribution is best
elseif length(idxs)=1
fprintf('%s Distribution is best\n',error_str(idxs))
elseif length(idxs)=length(Errors)
fprintf('All Distributions are equally good\n')
% e.g., Log and Rayleigh distributions are best
elseif length(idxs)=2
fprintf('%s and %s Distributions are best\n',error_str(idxs))
% e.g., Log, Exponential, and Rayleigh distributions are best
else
tmp = strjoin(error_str(idxs(1:end-1)),", ") + ", and " + error_str(idxs(end);
fprintf('%s Distributions are best',tmp)
end

추가 답변 (1개)

Benedict Comerford
Benedict Comerford 2020년 10월 7일
Hi Sindar
Thanks so much for that i think that will work just getting an error atm because (val) is undefined
  댓글 수: 2
Benedict Comerford
Benedict Comerford 2020년 10월 7일
All good it was just the min values
Sindar
Sindar 2020년 10월 7일
yup, had a typo / holdover from a first draft

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by