MATLAB Answers

0

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)

Asked by Eduardo Rocha on 26 Oct 2016
Latest activity Commented on by Eduardo Rocha on 28 Oct 2016
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

  1 Comments

Duplicate of earlier http://www.mathworks.com/matlabcentral/answers/308996-i-have-a-6266x1-cell-teste1-and-i-want-to-create-teste2 . As that one did not have an Answer (it was waiting for response to a Comment), that one has been closed and the Comment moved here.

Sign in to comment.

2 Answers

Answer by xiexiezaijian on 26 Oct 2016

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 Comments

It helped a bit but it didn't solve my problem, I have everything on "teste2" with "V". I did it like this:
teste2 = teste1;
for k=1:length(teste2)
if teste2{k} ~= 0;%determine whether the first cell element of teste2 is zero
teste2{k}='V';
elseif teste2{k} == 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{k}=0;
end
end
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'.
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?

Sign in to comment.


Answer by Walter Roberson
on 26 Oct 2016

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 Comments

Thank you for your help but its not working... To answer clearly to the questions of your first topic:
" Is it that case that for every teste1 entry, teste1(K), that there is a corresponding catricio1{K} and corresponding catpreco{K} entry?"
No, teste1(k) is an entry for teste2(k). catpreco is used to determine determine teste1 but not catracio1. Anyway, its not important to my question;
"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}?"
Yes. Both string cells only have those 5 categories (from "low" to "high") and the idea is to compare them and the end result should be a class number (0 to 5): if they are the same, the class is from 1 to 5, from both "low" to both "high"; if they are not, the class is 0. Actually, I want to check now if they have the same category but in another part of my program I may want to compare like if there is one with "low" and the other with "high", atributing a class number to this situation. And this is why I would prefer if the code would be in the same structure as it is on my question and first comment, so I can easily manipulate how and what to compare.
"But where test1(K) was non-zero you want to assign 'V' to teste2{K} ?"
Yes. "V" means "valid", and because I already had a class from the previous test (teste1), this row of the data I'm analyzing does not need to be tested again. This is why only the "0" are tested for teste2 - they were not explained by teste1. And if they are still not explained, they continue as 0.
Sorry for not being so clear before, I really appreciate your help. Can you try again please?
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.
"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).

Sign in to comment.



Translated by