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.

2 vues (au cours des 30 derniers jours)
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 commentaires
Muhammad Usman Saleem
Muhammad Usman Saleem le 4 Juin 2015
@dpb are you talking about that:
function mystr=censor(vec,str)
mystr= cellfun(strfind,vec,str);
end
again error is same?
Guillaume
Guillaume le 4 Juin 2015
Modifié(e) : Guillaume le 4 Juin 2015
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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Juin 2015
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 commentaires
krushnasamy subramaniyan
krushnasamy subramaniyan le 8 Juin 2015
error Undefined function or variable 'vec'. Error in censor (line 1) contains_str = false(1,length(vec));

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 7 Juin 2015
I would use John D'Errico's allwords http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords in combination with ismember() or setdiff().
  1 commentaire
Muhammad Usman Saleem
Muhammad Usman Saleem le 7 Juin 2015
@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...

Connectez-vous pour commenter.


dpb
dpb le 7 Juin 2015
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.

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by