how to make a function whose first input argument is a cell vector (row or column) of strings and whose second input argument is a string.

Hi every one; I am going to attempt that 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'.
I have made that program:
function mystr=censor(vec,str)
for i=1:length(vec)
if vec(i)==str
vec(i)=str;
else
mystr=vec;
end
end
end
but 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.
Kindly check my program whether i am doing correct or wrong. Please guide me about correction? Thanks in advance for assistance...

10 comentarios

It looks like you are submitting your function to some kind of automatic grading program, which simply tells you if it is right or wrong. Have you tried just running the program yourself in MATLAB, where you will get a more helpful error message?
@the cyclist problem is that i have to relay on the grader checking about wrong or right of my function. (1) Firstly please read the question i have posted above (2) Check whether i am going to do right which the question ask from me (3) then try to search the reseason of my corrections. Thanks
This duplicates the previous question at how-to-remove-that-element-from-cell-vector-which-is-identical-to-given-argument-or-contains-the-arg where I provided you some additional hints on how to solve the problem as asked.
As Cyclist says, debug your code directly while developing, don't just rely on the grader to say "right/wrong".
@dpb thanks for contributions.. Before posting that question i have already check that question. I am using different approach and getting an error. Please check my code and guide me where i need corrections? Thanks in advance
Well, those hints are where you need "corrections"; your "solution" is looking at the wrong idea on how to get the requested solution...
@Muhammad, this must be the fourth question I've seen by you where you appear to be throwing some random (very invalid!) code at the grader and hope to succeed. You don't appear to be testing your code at all because if you had you'd already found the obvious syntax errors in your code.
You also don't seem to be trying very hard before asking for help and in particular don't appear to think about algorithm before writing your code. For example, this particular problem clearly says that an element has to be removed if the 2nd argument is a substring of it. You don't even test for that in your code. Note that the grader (cody I presume) tells you which inputs your code fail with, so you have no excuse for not testing your code with these inputs.
@Guillaume thanks for criticize on me.Please check my all questions , then you know i do not get help directly .firstly i post my own create codes and gets suggestions to improve this... I am not programmer that why i share i own output and find corrections... Why we people always take negative every thing in first step?
Not so much that do, but...it's difficult for folks to help when it seems little (if any) of the advice proffered is followed and hence one tends to try to coach/persuade to cause a change in pattern in order to try to generate a more positive outcome than that observed to date.
If the data are as initially given in a cell array of strings (not a nested array as another respondent showed in his sample data) then I've pointed you to a very straightforward solution. As this is obviously coursework it would be a breach of etiquette if nothing else to provide much more directly at least until one sees a specific question regarding that implementation effort shown.
Again, you're hamstringing yourself if all you're doing is submitting code samples to the automated grader; use the tools of the development environment both interactively and thru the editor/debugger to debug your code and ensure it's working before you even think of submitting it to the grader; until you can see the correct output at your own command line you know there's no chance of it passing and it provides as you've seen, no useful additional information regarding anything within the code itself; it's just a pass|fail mark.
Again, the above is looking at the wrong piece of information; in the sample dataset the cell strings are lines of text of multiple words each and the word to be used as the target is only a single word. Hence, it's impossible for that to work as a match; the word while perhaps included within the text will not be "equal to" the text.
Go back to the other thread and reread my hints and follow the suggested course of action given therein and joy should ensue.
@dpb are you talking about that:
function mystr=censor(vec,str)
mystr= cellfun(strfind,vec,str);
end
again error is same?
The error with the above code would be
Error using strfind
Not enough input arguments.
which is a different error that you would have got with any other code you've posted. You would have seen that immediately if you'd tested it in matlab.
I'd recommend you do not use cellfun as its syntax is probably too complex for a beginner. If you do, note that the first argument must be a function handle and that the cell array inputs must all have the same size.
Use a loop instead.

Iniciar sesión para comentar.

 Respuesta aceptada

contains_str = false(1, length(vec));
for K = 1 : length(vec)
if ~isempty(strfind(vec{K}, str))
contains_str(K) = true;
end
end
mystr = {};
for K = 1 : length(vec)
if ~contains_str(K)
mystr(end+1) = vec(K);
end
end

2 comentarios

error Undefined function or variable 'vec'. Error in censor (line 1) contains_str = false(1,length(vec));

Iniciar sesión para comentar.

Más respuestas (2)

I would use John D'Errico's allwords http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords in combination with ismember() or setdiff().

1 comentario

@image thanks for contributions...... But the after getting hints from a lot of people i getting confuse what i have to do in this question...

Iniciar sesión para comentar.

Against better judgment but since Walter's already posted one solution, for the original problem description the suggested approach is-
>> 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?' };
>> s(cellfun(@isempty,strfind(s,'the')))
ans =
'Oh, say does that star-spangled banner yet wave'
>>
As noted, it's likely cellfun is more advanced than your course at the moment, so work backwards and see how to make the above without it using a loop instead.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by