Info

This question is closed. Reopen it to edit or answer.

I want to make a hierarchical system with some tests. However, I don't understand why my code does not work..

1 view (last 30 days)
"teste1" is a cell array (1 column) with a number from 0 to 5 in each row. The code for this is this one:
teste1 = cell(size(catconsumo));
teste1(:) = {'0'};
mask = strcmp(catconsumo, 'low') & strcmp(catpreco, 'low');
teste1(mask) = {'1'};
mask = strcmp(catconsumo, 'medium low') & strcmp(catpreco, 'medium low');
teste1(mask) = {'2'};
mask = strcmp(catconsumo, 'medium') & strcmp(catpreco, 'medium');
teste1(mask) = {'3'};
mask = strcmp(catconsumo, 'medium high') & strcmp(catpreco, 'medium high');
teste1(mask) = {'4'};
mask = strcmp(catconsumo, 'high') & strcmp(catpreco, 'high');
teste1(mask) = {'5'};
However, not even my thesis supervisor can explain to me why this works. Since the strcmp will return a value of 0 or 1, there should be always "teste1(0)" or "teste1(1)", so we think it should always write on row 1. Can somebody explain me firstly why this works?!
Regardless of understanding why it works, I tried to use for my hierarchical system:
teste2 = teste1;
for k=2:(length(Preos2013S1)-1)
if teste2{k} == '0'
mask = strcmp(catracio1, 'low') & strcmp(catpreco, 'high');
teste2(mask) = {'A'};
mask = strcmp(catracio1, 'medium low') & strcmp(catpreco, 'medium high');
teste2(mask) = {'B'};
mask = strcmp(catracio1, 'medium') & strcmp(catpreco, 'medium');
teste2(mask) = {'C'};
mask = strcmp(catracio1, 'medium high') & strcmp(catpreco, 'medium low');
teste2(mask) = {'D'};
mask = strcmp(catracio1, 'high') & strcmp(catpreco, 'low');
teste2(mask) = {'E'};
else
teste2{k}={'V'};
end
end
Now what happens is that it ignores the "if" condition: even if there is not a 0 in the row, the result can be from 'A' to 'E'.
So, I tried with (I think) is the most logical way:
teste2 = teste1;
for k=2:(length(Preos2013S1)-1)
if teste2{k} == '0'
if strcmp(catracio1, 'low') & strcmp(catpreco, 'high')
teste2{k} = {'A'};
elseif strcmp(catracio1, 'medium low') & strcmp(catpreco, 'medium high')
teste2{k} = {'B'};
elseif strcmp(catracio1, 'medium') & strcmp(catpreco, 'medium')
teste2{k} = {'C'};
elseif strcmp(catracio1, 'medium high') & strcmp(catpreco, 'medium low')
teste2{k} = {'D'};
elseif strcmp(catracio1, 'high') & strcmp(catpreco, 'low')
teste2{k} = {'E'};
end
else
teste2{k}={'V'};
end
end
Now, results are only or '0' or 'V'. So I even tried like this:
teste2 = teste1;
for k=2:(length(Preos2013S1)-1)
if teste2{k} == '0' & strcmp(catracio1, 'low') & strcmp(catpreco, 'high')
teste2{k} = {'A'};
elseif teste2{k} == '0' & strcmp(catracio1, 'medium low') & strcmp(catpreco, 'medium high')
teste2{k} = {'B'};
elseif teste2{k} == '0' & strcmp(catracio1, 'medium') & strcmp(catpreco, 'medium')
teste2{k} = {'C'};
elseif teste2{k} == '0' & strcmp(catracio1, 'medium high') & strcmp(catpreco, 'medium low')
teste2{k} = {'D'};
elseif teste2{k} == '0' & strcmp(catracio1, 'high') & strcmp(catpreco, 'low')
teste2{k} = {'E'};
else
teste2{k}={'V'};
end
end
Even worst: now everything is 'V'. Can somebody please help me understanding what the hell is wrong?! I just want to use the strcmp in the rows where teste2 is '0'. If its not 0, I want a 'V', regardless of 'strcmp' results.

Answers (0)

Community Treasure Hunt

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

Start Hunting!