If loop with character and two conditions

Hello,
I am trying to create a loop, where I have ran two hypothesis tests, (ADF and KPSS test). And I want matlab to return the concltion in the command window, there is three outcomes, based on the conclution from the test.
This is what I got so far, but the problem is that in the case where the variable is not equal matlab returns: "Arrays have incompatible sizes for this operation."...
% Stationarity conclution loop
if t_adf_fdiff.reject == t_kpss_fdiff.reject
disp('Inconclusive test');
if isequal(t_adf_fdiff.reject, 'reject H0') &&...
isequal(t_kpss_fdiff.reject, 'do not reject H0')
disp(['The time series does not containt a unit root, and are '...
'stationary']);
if isequal(t_adf_fdiff.reject, 'do not reject H0') &&...
isequal(t_kpss_fdiff.reject, 'reject H0')
disp(['The time series containt a unit root, and are not'...
'stationary']);
end
end
end

Answers (1)

J Chen
J Chen on 13 Apr 2021
Edited: J Chen on 13 Apr 2021
Use
strcmp(t_adf_fdiff.reject,t_kpss_fdiff.reject)
insteafd of t_adf_fdiff.reject == t_kpss_fdiff.reject. The two if statements under the top if statement are meaningless since t_adf_fdiff.reject will be always equal to t_kpss_fdiff.reject.

4 Comments

Thank you! This resolves the error, but my next problem is that the loop does not display anything in the case where they are unequal..
Changed to:
if strcmp(t_adf.reject,t_kpss_level.reject)
disp('Inconclusive test');
if isequal(t_adf.reject, 'reject H0') &&...
isequal(t_kpss_level.reject, 'do not reject H0')
disp(['The time series does not containt a unit root, and are '...
'stationary']);
if isequal(t_adf.reject, 'do not reject H0') &&...
isequal(t_kpss_level.reject, 'reject H0')
disp(['The time series containt a unit root, and are not'...
'stationary']);
end
end
end
Your script will not display anything as it is since the following two if statements will never be true:
if isequal(t_adf.reject, 'reject H0') &&...
isequal(t_kpss_level.reject, 'do not reject H0')
if isequal(t_adf.reject, 'do not reject H0') &&...
isequal(t_kpss_level.reject, 'reject H0')
Isn´t for example the first example...
if isequal(t_adf.reject, 'reject H0') &&...
isequal(t_kpss_level.reject, 'do not reject H0')
The same as saying: "if t_adf = reject and t_kpss = do not reject"
In that case this statement can be true.
The strcmp(t_adf.reject,t_kpss_level.reject) will not be true in the first place in this case.

Sign in to comment.

Asked:

on 13 Apr 2021

Commented:

on 13 Apr 2021

Community Treasure Hunt

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

Start Hunting!