Skipping iteration on child throwing a warning
1 回表示 (過去 30 日間)
古いコメントを表示
I have a script X that has:
for x = 1:5000
output{x} = Script_Y(input(:,:,:,x));
end
Script_Y has a particular loop (among other code):
for alpha = 1:(size(input, 3)-1)
output_preprocessed = Script_Z(input(:,:,alpha), input(:,:,alpha+1));
end
% Later, some more operations are committed on output_preprocessed to give
% the function output.
Script_Z has a loop (among other code):
% Some operations are committed on function inputs to get input_postprocessed_1 and input_postprocessed_2
for k = 1:200
x = input_postprocessed_1/input_postprocessed_2
% More steps to make x precise %
end
% Later, some more operations are committed on x to give
% the function output.
It might occur that for some k, input_postprocessed_1 is singular or near-singular and as soon as Matlab throws this warning, I want to skip the particular iteration in the master loop at my script X! For that x, output{x} shall return NULL or nothing - either sufices for me.
How to go about?
2 件のコメント
Image Analyst
2023 年 9 月 28 日
What exactly is the warning? Please paste back here all the orange or red text that you see when it produces the warning.
Bruno Luong
2023 年 9 月 28 日
Actually it input_postprocessed_2 at the right division and the warning is likely the infamous
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = XXXXXX.
採用された回答
Bruno Luong
2023 年 9 月 28 日
編集済み: Bruno Luong
2023 年 9 月 28 日
Modification of script_Z
for k = 1:200
lastwarn('',''); % clean the warning state
x = input_postprocessed_1/input_postprocessed_2;
[~,warnid] = lastwarn;
if strcmp(warnid, 'MATLAB:singularMatrix');
x = []; % I assume this is output_preprocessed returned to script Y
break
end
% More steps to make x precise %
end
script_Y
for alpha = 1:(size(input, 3)-1)
output_preprocessed = Script_Z(input(:,:,alpha), input(:,:,alpha+1));
if isempty(output_preprocessed)
break
end
end
script_X
for x = 1:5000
output{x} = Script_Y(input(:,:,:,x));
% output{x} is empty if the division fails ( input_postprocessed_2is
% singular)
end
0 件のコメント
その他の回答 (1 件)
Image Analyst
2023 年 9 月 28 日
You can use try catch:
for k = 1 : 200
try
% In the try block put the code that might throw an error.
catch ME
% If you get here an error occurred.
continue; % To continue with next iteration, or use "break" to exit the loop.
end
end
1 件のコメント
Bruno Luong
2023 年 9 月 28 日
編集済み: Bruno Luong
2023 年 9 月 28 日
warning cannot be branched by try/catch, only error can.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!