compare two strings of cell array at different length and replace the existing string with a new set of strings
Show older comments
I need to check the elements of A and if the elements of A contains elements of B. I need to replace the elements of A with the elements of C only if the element of B is present in A.
I have three sets of cell array strings of different length as follows:
>> A={'apple';'ant';'book';'apple';'boy';'apple';'ant'};
>> B={'apple';'ant';'book'};
>> C={'apple1';'ant1';'book1'};
I need to check whether the elements of B are used in the elements of A. I have used several MATLAB inbuilt functions to check the elements but, I am not able to obtain the expected output. So, I am comparing strings of A and B with the following code:
X = zeros(size(A));
for i = 1:numel(B)
X(strncmp(A,B{i},3)) = i;
end
Here, I am getting the output of X as
X =
1
2
3
1
0
1
2
Now, I need to check the elements of A and if the elements of A contains elements of B. I need to replace the elements of A with the elements of C only if the element of B is present in A.
Here, I am getting the position of each element of B but not the logical indices of A. In simpler words, I need the output in the form of 1's and 0's. So, that I can replace the elements of A with the elements of C
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!