how to remove that element from cell vector which is identical to given argument or contains the argument as a substring. And return cell vector without that argument.

i use this....but it doesn't work
function y = censor(str,c1)
origStr = str;
oldSubstr = c1;
newSubstr = '';
y = strrep(origStr, oldSubstr, newSubstr)
Your function made an error for argument(s)
{ 'Oh, say can you see by the dawn''s early light', 'What so proudly we hailed at the twilight''s last gleaming?', 'Whose broad stripes and bright stars thru the perilous fight,', 'O''er the ramparts we watched were so gallantly streaming?', 'And the rocket''s red glare, the bombs bursting in air,', 'Gave proof through the night that our flag was still there.', 'Oh, say does that star-spangled banner yet wave', 'O''er the land of the free and the home of the brave?' }, 'the'
Your solution is _not_ correct.

6 comentarios

For what definition of "doesn't work?" Looks like an automated homework grader output; perhaps the requirement was that only complete words should be removed, not substrings. (HINT: What happens to the word "there" in your solution and what _should) happen by the assignment definition?)
this is my question...
Write a function called censor whose first input argument is a cell vector (row or column) of strings and whose second input argument is a string. It removes each element of the cell vector whose string is either identical to the second input argument or contains the second argument as a substring. The resulting cell vector is returned as the only output argument. For example, suppose each element of the first argument refers to one line of the first verse of the US national anthem and the second argument is 'the'. Then the function would return a cell vector with a single element containing the string: 'Oh, say does that star-spangled banner yet wave'.
Ah...that's a different design requirement altogether than what your code is trying to solve for...
You don't need a substitution for the matched string here, you need a location logical vector that tells you which cell in the input cell array does or, perhaps more directly, does not contain the magic word so you can return that list and thence the cells of that list.
HINT:
doc cellfun
doc isempty
doc strfind
can be combined in a one-liner to solve the problem as given.
ADDENDUM If cellfun is more advanced than you've progressed so far, then you can replace it with a loop around the result of the other two on the inputs.
As a hint on learning to develop in Matlab while still new, use
help
to see a list of the various areas; in this case the interesting functions are likely first of all under
help strfun
Look at those quick descriptions and see which tend to return what you need here and then experiment some at the keyboard in the command window on the various results of the likely candidates for your or some sample data to see specifically what the various alternatives return as results. After that exercise, it should then become apparent how to use the remaining hints above...
@dpb are you talking about that:
function mystr=censor(vec,str)
mystr= cellfun(strfind,vec,str);
end
again error is same?
dpb
dpb el 4 de Jun. de 2015
Editada: dpb el 4 de Jun. de 2015
Well, not quite, no...but it's a start towards the answer, yes.
You've got a couple of problems here yet; all of which can be resolved with a combination of using the documentation and the suggested technique of working interactively to understand what the result of each step is in sequence, precisely.
I'd note that Guilliame made a comment in the other thread re: cellfun that mirrors mine above; you'll have to decide whether this gives away to the instructor you didn't come up with this solution on your own or not, but as he (and the doc) say, the syntax isn't correct as it needs a function handle as the first argument, but...
If you had tried, you'd have already discovered that you don't need cellfun yet; strfind works with the cell input and string as given in the assignment; first see how that works and then attack the rest of the problem getting that form of the information about the pattern to what is required. Again note, I listed another function in there that should become apparent as to why...
ADDENDUM Again, keep your eye on the goal first and work logically towards accomplishing that objective in a planned sequence of steps that mimic what you would do to solve the problem if were to do it with pencil and paper. Refer again to my first note--the idea is to find the lines (given as array of cellstrings per problem description) which contain the magic word. Having those, those are the cells that are not to be returned; conversely one can isolate the obverse indicator--namely, those cells which do not contain the target string; in that case those are the cells which are to be returned.
As suggested, if you use the suggested search at the command line to see how Matlab deals with the two cases of found/not found, that should lead you to the next step which then leads to the last. And, at that point, voila!--you're done.

Iniciar sesión para comentar.

Respuestas (1)

str={'hi' 'me' 'if' {'yes' 'no' 'hi'} {'for' 'while' 'all'} 'hi'}; %example
element='hi'; %remove 'hi'
str(strcmp(str,element))='';
for i=1:length(str)
if iscell(str{i})
str{i}(strcmp(str{i},element))='';
end
end
disp(str)

5 comentarios

@Jesus i am using that code:
function mystr=censor(vec,str)
for i=1:length(vec)
if vec(i)==str
vec(i)=str;
else
mystr=vec;
end
end
end
but getting that error
Feedback: Your program made an error for argument(s) { 'Oh, say can you see by the dawn''s early light', 'What so proudly we hailed at the twilight''s last gleaming?', 'Whose broad stripes and bright stars thru the perilous fight,', 'O''er the ramparts we watched were so gallantly streaming?', 'And the rocket''s red glare, the bombs bursting in air,', 'Gave proof through the night that our flag was still there.', 'Oh, say does that star-spangled banner yet wave', 'O''er the land of the free and the home of the brave?' }, 'the'
Your solution is _not_ correct.
Where i need corrections? thanks in advance
this is correct if input argument is cell...
@Darshan Thanks for contributions.. What is meant by "cell vector" Are you talking about? gives some hint in the form of code..
function mystr=censor(str,substr)
str={str};
element=substr;
str(strcmp(str,element))='';
for i=1:length(str)
if iscell(str{i})
str{i}(strcmp(str{i},element))='';
end
end
I have used that error is same?
@Muhammad, You've been told again and again to test your code.
The error is never the same and would be obvious if you'd run your code in matlab. All the grader is telling you is that you've made an error, matlab would tell you what the error is.
As said before, just throwing code at the problem is not going to help you. You need to think first about what your code is going to do.

Iniciar sesión para comentar.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 1 de Jun. de 2015

Editada:

dpb
el 4 de Jun. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by