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.

1 Ansicht (letzte 30 Tage)
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 Kommentare
Muhammad Usman Saleem
Muhammad Usman Saleem am 4 Jun. 2015
@dpb are you talking about that:
function mystr=censor(vec,str)
mystr= cellfun(strfind,vec,str);
end
again error is same?
dpb
dpb am 4 Jun. 2015
Bearbeitet: dpb am 4 Jun. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH am 1 Jun. 2015
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 Kommentare
Muhammad Usman Saleem
Muhammad Usman Saleem am 4 Jun. 2015
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?
Guillaume
Guillaume am 4 Jun. 2015
Bearbeitet: Guillaume am 4 Jun. 2015
@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.

Melden Sie sich an, um zu kommentieren.

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by