Info

此问题已关闭。 请重新打开它进行编辑或回答。

I have a column cell array "teste1" and I want to create "teste2": if "teste1" is not 0, I want a 'V'; if "teste1" is 0, I want to compare 2 other different string cells and the result would be from 1 to 5 (or 0, if no condition was satisfied)

1 次查看(过去 30 天)
How can I proceed? I tried like this but "Undefined operator '~=' for input arguments of type 'cell'" :
teste2 = teste1;
if teste2(:)~=0
teste2(:)='V';
elseif teste2(:) == 0
mask = strcmp(catracio1, 'low') & strcmp(catpreco, 'low');
teste2(mask) = {'1'};
mask = strcmp(catracio1, 'medium low') & strcmp(catpreco, 'medium low');
teste2(mask) = {'2'};
mask = strcmp(catracio1, 'medium') & strcmp(catpreco, 'medium');
teste2(mask) = {'3'};
mask = strcmp(catracio1, 'medium high') & strcmp(catpreco, 'medium high');
teste2(mask) = {'4'};
mask = strcmp(catracio1, 'high') & strcmp(catpreco, 'high');
teste2(mask) = {'5'};
else
teste2(:)=0;
end

回答(2 个)

xiexiezaijian
xiexiezaijian 2016-10-26
I think the problem is in these lines:
if teste2(:)~=0
teste2(:)='V';
but I get really confused about what you want to do:
1. isempty(teste2);%determine whether teste2 is empty
2. teste2{1} ~= 0;%determine whether the first cell element of teste2 is zero
I hope this will help;
  3 个评论
xiexiezaijian
xiexiezaijian 2016-10-26
Sorry, I don't understand some variables(catracio1, catpreco) in your code; I guess you want change value of some cell elements to 'V' '1', '2', '3', '4' or '5'; Maybe you could get idea from the example below;
a = cell(1,9);
a(cellfun(@(x) ~isequal(x,0),a)) = {'V'}
maybe you should replace 'isequal' to 'strcmp'.
Eduardo Rocha
Eduardo Rocha 2016-10-27
catracio1 and catpreco are string cells of the same size of "teste1" and they will be used to determine "teste2".
"teste1" is a cell with one column and many rows, with numbers from 0 to 5. I want "teste2(K)" to be:
'V' if "teste1(K)" is NOT 0;
If "teste1(K)" is 0, I want to make a comparison between "catracio1(K)" and "catpreco(K)": 'low' and 'low' should be '1' for "teste2(k)", "medium low" and "medium low" should be '2' and so on. If none of the conditions are satisfied, the '0' remains as result for "teste2(K)".
This last comment you wrote is only for the "V", what about for the results 0 to 5? Can you help me please?

Walter Roberson
Walter Roberson 2016-10-26
You need to answer the questions I asked you in your earlier post of the same question:
What are you data structures here? Is it that case that for every teste1 entry, teste1(K), that there is a corresponding catricio1{K} and corresponding catpreco{K} entry? And is it correct that for the locations where test1(K) is 0, you want to compare the two corresponding string entries and if they are equal then you want to assign a character corresponding to the class number to teste2{K} ? And is it the case that if the two strings are not equal, or the two strings are something other than one of those 5, that you want to assign '0' to teste2{K} ? But where test1(K) was non-zero you want to assign 'V' to teste2{K} ?
  7 个评论
Walter Roberson
Walter Roberson 2016-10-27
When teste1(K) is 0, catpreco{K} needs to be examined, but which entry in catracio1 needs to be examined then? You say here that catracio1{K} does not correspond to catpreco{K} so I do not know which entry in catracio1 to look at .
When I say "corresponds", I do not mean "is caused by", just that there the positions match up. For example when looking at catpreco{K}, you would not be wanting to look to see if there is any entry in catracio1 that matches, you probably only want to look at one particular one, but here you say that it would not be the K'th entry.
Eduardo Rocha
Eduardo Rocha 2016-10-28
"When teste1(K) is 0, catpreco{K} needs to be examined, but which entry in catracio1 needs to be examined then? "
Same row of catpreco (row number k).
"You say here that catracio1{K} does not correspond to catpreco{K} so I do not know which entry in catracio1 to look at ."
catracio1 and catpreco have no relation, they are independent cells. I said that catpreco is used to determine teste1 but catracio1 is NOT used to determine teste1. Now, I need to use catpreco and catracio1 to determine teste2.
"For example when looking at catpreco{K}, you would not be wanting to look to see if there is any entry in catracio1 that matches, you probably only want to look at one particular one, but here you say that it would not be the K'th entry."
False.. I want to compare for k'th entry, row by row. catracio1(k) and catpreco(k).

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by